123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- namespace XRTool.Util
- {
- public delegate void TimerHandler();
- public delegate void TimerArgsHandler(System.Object[] args);
- public class Timer
- {
- public TimerHandler Handler;
- public TimerArgsHandler ArgsHandler;
- public float Frequency;
- public int Repeats;
- public System.Object[] Args;
- public float LastTickTime;
- public event Action OnComplete;
- public event Action OnDestroy;
- public event Action OnFinsh;
- public Timer() { }
-
-
-
-
-
-
-
-
- public Timer(TimerHandler Handler, TimerArgsHandler ArgsHandler, float frequency, int repeats, System.Object[] Args)
- {
- this.Handler = Handler;
- this.ArgsHandler = ArgsHandler;
- this.Frequency = frequency;
- this.Repeats = repeats == 0 ? 1 : repeats;
- this.Args = Args;
- this.LastTickTime = Time.time;
- }
- public void Notify()
- {
- Handler?.Invoke();
- ArgsHandler?.Invoke(Args);
- OnComplete?.Invoke();
- if (Repeats == 0)
- {
- OnFinsh?.Invoke();
- }
- }
-
-
-
- public void CleanUp()
- {
- Handler = null;
- ArgsHandler = null;
- Repeats = 1;
- Frequency = 0;
- OnDestroy?.Invoke();
- OnFinsh?.Invoke();
- OnDestroy = null;
- OnComplete = null;
- }
- }
-
-
-
-
-
-
- public class TimerMgr : UnitySingleton<TimerMgr>
- {
- private List<Timer> _Timers;
- protected override void Awake()
- {
- DontDestroyOnLoad(gameObject);
- if (_Timers == null)
- {
- _Timers = new List<Timer>();
- }
- base.Awake();
- }
-
-
-
-
-
-
- public Timer CreateTimer(TimerHandler callBack, float time, int repeats = 1)
- {
- return Create(callBack, null, time, repeats);
- }
- public Timer CreateTimer(TimerArgsHandler callBack, float time, int repeats, params System.Object[] args)
- {
- return Create(null, callBack, time, repeats, args);
- }
- private Timer Create(TimerHandler callBack, TimerArgsHandler callBackArgs, float time, int repeats, params System.Object[] args)
- {
- Timer timer = new Timer(callBack, callBackArgs, time, repeats, args);
- _Timers.Add(timer);
- return timer;
- }
- public Timer DestroyTimer(Timer timer)
- {
- if (timer != null)
- {
- _Timers.Remove(timer);
- timer.CleanUp();
- timer = null;
- }
- return timer;
- }
- public void ClearAll()
- {
- if (_Timers != null)
- {
- for (int i = 0; i < _Timers.Count; i++)
- {
- _Timers[i].CleanUp();
- }
- _Timers.Clear();
- }
- }
-
-
-
- void Update()
- {
- if (_Timers.Count != 0)
- {
- for (int i = _Timers.Count - 1; i >= 0 && i < _Timers.Count; i--)
- {
- Timer timer = _Timers[i];
- float curTime = Time.time;
- if (timer.Frequency + timer.LastTickTime > curTime)
- {
- continue;
- }
- timer.LastTickTime = curTime;
- timer?.Notify();
- timer.Repeats--;
-
- if (timer.Repeats == 0)
- {
- DestroyTimer(timer);
- }
- }
- }
- }
- }
- }
|