1、Timer timer = new Timer(); 创建时间管理器 参数(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true) time:时间值、timeUnit 时间单位(帧率、秒、厘秒、毫秒)、ignoreTimeScale 忽略时间缩放(可以使游戏暂停更容易实现)、autoStart 自动开始计时
2、timer.loop = true; // 循环 timer.Finished +=method; // 回调函数 timer.Pause(); // 暂停
using System.Collections.Generic; using UnityEngine; namespace Epitome { public delegate void FinishedHandler(); public class Timer { TimerManager.TimerState timer; public bool Running { get { return timer.Running; } } public bool Paused { get { return timer.Paused; } } public bool Loop { set { timer.HasRepeat = value; } get { return timer.HasRepeat; } } public event FinishedHandler Finished; public Timer(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true) { timer = TimerManager.Instance.CreateTimer(time, timeUnit, ignoreTimeScale); timer.Finished += TimerFinished; if (autoStart) timer.Start(); } public void Start() { timer.Start(); } public void Stop() { timer.Stop(); } public void Pause() { timer.Pause(); } public void UnPause() { timer.UnPause(); } public void TimerFinished() { FinishedHandler handler = Finished; if (handler != null) handler(); } } public enum TimeUnit { FrameRate, // 帧率 Second, // 秒 CentiSecond, // 厘秒:是一秒的百分之一(0.01秒) MilliSecond, // 毫秒:是一秒的千分之一(0.001秒) } public class TimerManager : MonoSingleton<TimerManager> { public class TimerState { bool running; bool paused; bool stopped; public bool Running { get { return running; } } public bool Paused { get { return paused; } } public event FinishedHandler Finished; private TimeUnit timeUnit; private float delayTime; // 延迟时间 private float attackTime; // 启动时间 private float currentTime; // 当前时间 public bool HasRepeat; // 一直重复 public bool ignoreTimeScale { get; private set; } // 忽略时间缩放 public TimerState(float time, TimeUnit unit, bool ignore) { timeUnit = unit; ignoreTimeScale = ignore; delayTime = time; ResetState(); } private void ResetState() { switch (timeUnit) { case TimeUnit.FrameRate: currentTime = 0.0f; break; case TimeUnit.Second: case TimeUnit.CentiSecond: case TimeUnit.MilliSecond: if (!ignoreTimeScale) currentTime = 0.0f; else currentTime = Time.realtimeSinceStartup; break; } attackTime = delayTime + currentTime; } public void UpdateTime(float time) { time = ignoreTimeScale ? time - currentTime : time; if (running) { if (paused) return; switch (timeUnit) { case TimeUnit.FrameRate: currentTime += 1; break; case TimeUnit.Second: currentTime += time; break; case TimeUnit.CentiSecond: currentTime += time * 100; break; case TimeUnit.MilliSecond: currentTime += time * 1000; break; } if (currentTime >= attackTime) { if (HasRepeat) { ResetState(); } else { Stop(); } FinishedHandler handle = Finished; if (handle != null) { handle(); } } } } public void Start() { running = true; } public void Stop() { stopped = true; running = false; } public void Pause() { paused = true; } public void UnPause() { paused = false; } } private List<TimerState> timerList = new List<TimerState>(); private void Update() { for (int i = 0; i < timerList.Count ; i++) { timerList[i].UpdateTime(timerList[i].ignoreTimeScale ? Time.realtimeSinceStartup : Time.deltaTime); } } public TimerState CreateTimer(float time, TimeUnit timeUnit,bool ignoreTimeScale) { TimerState newTimer = new TimerState(time, timeUnit, ignoreTimeScale); timerList.Add(newTimer); return newTimer; } public void ClearTimer() { } public void ClearAllTimer() { } } }
讯享网
使用案例
讯享网public class text : MonoBehaviour { // Use this for initialization void Start () { Time.timeScale = 3; Timer timer = new Timer(1, TimeUnit.Second); //第三个参数是否忽略时间缩放带来的影响 timer.Loop = true; // 设置可循环 timer.Finished += rw; } private void rw() { Debug.Log("你好"); } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/104849.html