Unity计时器系统
很好用,方便
using System; using System.Collections.Generic; using UnityEngine; public class TimerManager : MonoBehaviour { private List<Timer> _usedTimes = new List<Timer>(); private Queue<Timer> _canceledTimes = new Queue<Timer>(); public void Update() { int count = _usedTimes.Count; for (int i = count -1; i >= 0; i--) { _usedTimes[i].Uptate(); } } public void CancelTimer(Timer timer) { _usedTimes.Remove(timer); if(_canceledTimes.Count < 10) { timer.Cancel(); _canceledTimes.Enqueue(timer); } } public Timer AddTimer(float delayTime, Action excuteFunc) { return AddTimer(delayTime, 1, delayTime, false, excuteFunc, null, null); } public Timer AddTimer(float duration, int loopTimes, Action excuteFunc) { return AddTimer(duration, loopTimes, duration, false, excuteFunc, null, null); } public Timer AddTimer(float duration, float loopTimes, float delayTime, Action excuteFunc) { return AddTimer(duration, loopTimes, delayTime, false, excuteFunc, null, null); } public Timer AddTimer(float duration, float loopTimes, float delayTime, bool isRealTime, Action excuteFunc) { return AddTimer(duration, loopTimes, delayTime, isRealTime, excuteFunc, null, null); } /// <summary> /// /// </summary> /// <param name="duration">2次执行的时间间隔</param> /// <param name="loopTimes">循环几次</param> /// <param name="delayTime">第一次的延迟</param> /// <param name="isRealTime">是否真实时间</param> /// <param name="excuteFunc">执行体</param> /// <param name="completeFunc">完成调用</param> /// <param name="finishFunc">取消时调用</param> /// <returns></returns> public Timer AddTimer(float duration, float loopTimes, float delayTime, bool isRealTime, Action excuteFunc, Action completeFunc, Action finishFunc) { Timer timer = null; if (_canceledTimes.Count > 0) { timer = _canceledTimes.Dequeue(); } else timer = new Timer(this); timer.Create(duration, loopTimes, delayTime, isRealTime, excuteFunc, completeFunc, finishFunc); _usedTimes.Add(timer); return timer; } }
using System; using UnityEngine; public class Timer { public Timer(TimerManager tm) { timerManager = tm; } public float duration; public float loopTimes; public float delayTime; public bool isRealTime; public Action completeFunc; public Action finishFunc; public Action excuteFunc; private TimerManager timerManager; private float _lastDoneTime; private float _startTime; public void Create(float duration, float loopTimes, float delayTime, bool isRealTime, Action excuteFunc, Action completeFunc, Action finishFunc) { this.duration = duration; this.loopTimes = loopTimes; this.delayTime = delayTime; this.isRealTime = isRealTime; this.completeFunc = completeFunc; this.finishFunc = finishFunc; this.excuteFunc = excuteFunc; float curTime = GetTime(); _startTime = curTime + delayTime; } public void Uptate() { float curTime = GetTime(); if (_lastDoneTime < 0) { // 第一次执行 if(curTime >= _startTime) { if (excuteFunc != null) excuteFunc(); _lastDoneTime = curTime; if(loopTimes != -1) { loopTimes--; if(loopTimes == 0) { if (finishFunc != null) finishFunc(); timerManager.CancelTimer(this); } } } } else { // 循环执行 if (curTime - _lastDoneTime >= duration) { if (excuteFunc != null) excuteFunc(); _lastDoneTime = curTime; if (loopTimes != -1) { loopTimes--; if (loopTimes == 0) { if (finishFunc != null) finishFunc(); timerManager.CancelTimer(this); } } } } } private float GetTime() { if (isRealTime) return Time.realtimeSinceStartup; else return Time.time; } public void Cancel() { _lastDoneTime = -1; _startTime = 0; } }