using UnityEngine; using UnityEngine.Events; /// /// 计时器 /// ZhangYu 2018-04-08 /// code from: https://segmentfault.com/a/1190000015325310 /// public class Timer : MonoBehaviour { // delay in second public float delay = 0; // interval in second public float interval = 1; // repeat count public int repeatCount = 1; // automatically start public bool autoStart = false; // automatically destroy public bool autoDestory = true; // current time public float currentTime = 0; // current count public int currentCount = 0; // event when interval reached public UnityEvent onIntervalEvent; // event when timer completed public UnityEvent onCompleteEvent; // callback delegate public delegate void TimerCallback(Timer timer); // last interval time private float lastTime = 0; // interval callback private TimerCallback onIntervalCall; // complete callback private TimerCallback onCompleteCall; private void Start() { enabled = autoStart; } private void FixedUpdate() { if (!enabled) return; addInterval(Time.deltaTime); } /// add interval private void addInterval(float deltaTime) { currentTime += deltaTime; if (currentTime < delay) return; if (currentTime - lastTime >= interval) { currentCount++; lastTime = currentTime; if (repeatCount <= 0) { // repeate forever if (currentCount == int.MaxValue) reset(); if (onIntervalCall != null) onIntervalCall(this); if (onIntervalEvent != null) onIntervalEvent.Invoke(); } else { if (currentCount < repeatCount) { if (onIntervalCall != null) onIntervalCall(this); if (onIntervalEvent != null) onIntervalEvent.Invoke(); } else { stop(); if (onCompleteCall != null) onCompleteCall(this); if (onCompleteEvent != null) onCompleteEvent.Invoke(); if (autoDestory && !enabled) Destroy(this); } } } } public void start() { enabled = autoStart = true; } public void start(float time, TimerCallback onComplete) { start(time, 1, null, onComplete); } public void start(float interval, int repeatCount, TimerCallback onComplete) { start(interval, repeatCount, null, onComplete); } public void start(float interval, int repeatCount, TimerCallback onInterval, TimerCallback onComplete) { this.interval = interval; this.repeatCount = repeatCount; onIntervalCall = onInterval; onCompleteCall = onComplete; reset(); enabled = autoStart = true; } public void stop() { enabled = autoStart = false; } public void reset() { lastTime = currentTime = currentCount = 0; } public void restart() { reset(); start(); } }