TimerMgr.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. namespace XRTool.Util
  5. {
  6. public delegate void TimerHandler();
  7. public delegate void TimerArgsHandler(object[] args);
  8. public class Timer
  9. {
  10. public TimerHandler Handler; //无参的委托
  11. public TimerArgsHandler ArgsHandler; //带参数的委托
  12. public float Frequency; //时间间隔
  13. public int Repeats; //重复次数
  14. public object[] Args;
  15. public float LastTickTime;
  16. public event Action OnComplete; //计时器完成一次工作
  17. public event Action OnDestroy; //计时器被销毁
  18. public event Action OnFinsh; //计时器结束工作了
  19. public Timer() { }
  20. /// <summary>
  21. /// 创建一个时间事件对象
  22. /// </summary>
  23. /// <param name="Handler">回调函数</param>
  24. /// <param name="ArgsHandler">带参数的回调函数</param>
  25. /// <param name="frequency">时间内执行</param>
  26. /// <param name="repeats">重复次数</param>
  27. /// <param name="Args">参数 可以任意的传不定数量,类型的参数</param>
  28. public Timer(TimerHandler Handler, TimerArgsHandler ArgsHandler, float frequency, int repeats, object[] Args)
  29. {
  30. this.Handler = Handler;
  31. this.ArgsHandler = ArgsHandler;
  32. this.Frequency = frequency;
  33. this.Repeats = repeats == 0 ? 1 : repeats;
  34. this.Args = Args;
  35. this.LastTickTime = Time.time;
  36. }
  37. public void Notify()
  38. {
  39. Handler?.Invoke();
  40. ArgsHandler?.Invoke(Args);
  41. OnComplete?.Invoke();
  42. }
  43. /// <summary>
  44. /// 清理计时器,初始化参数 同时清理事件
  45. /// </summary>
  46. public void CleanUp()
  47. {
  48. Handler = null;
  49. ArgsHandler = null;
  50. Repeats = 1;
  51. Frequency = 0;
  52. OnDestroy?.Invoke();
  53. OnFinsh?.Invoke();
  54. OnDestroy = null;
  55. OnComplete = null;
  56. }
  57. /// <summary>
  58. /// 立即执行回调
  59. /// isBreak = false的时候需要根据是否继续执行,传time
  60. /// isBreak = true,可不传time
  61. /// </summary>
  62. /// <param name="time">当前的时间,Time.time</param>
  63. /// <param name="isBreak">执行完之后是否销毁计时器</param>
  64. public void AwakeNow(float time = 0, bool isBreak = true)
  65. {
  66. try
  67. {
  68. ///唤醒可能会有异常
  69. Notify();
  70. }
  71. catch (Exception ex)
  72. {
  73. UnityLog.LogException(ex);
  74. UnityLog.LogError(ex.ToString());
  75. }
  76. finally
  77. {
  78. if (isBreak)
  79. {
  80. OnFinsh?.Invoke();
  81. TimerMgr.Instance.DestroyTimer(this);
  82. }
  83. else
  84. {
  85. if ((--Repeats) == 0)
  86. {
  87. OnFinsh?.Invoke();
  88. TimerMgr.Instance.DestroyTimer(this);
  89. }
  90. else
  91. {
  92. LastTickTime = time;
  93. }
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// 根据当前时间判断是否要触发触发器
  99. /// </summary>
  100. /// <param name="time"></param>
  101. public void Update(float time)
  102. {
  103. if (Frequency + LastTickTime <= time)
  104. {
  105. AwakeNow(time, false);
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// 计时器
  111. /// 添加一个计时事件
  112. /// 删除一个计时事件
  113. /// 更新计时事件
  114. /// </summary>
  115. public class TimerMgr : UnitySingleton<TimerMgr>
  116. {
  117. private List<Timer> _Timers;//时间管理器
  118. protected override void Awake()
  119. {
  120. DontDestroyOnLoad(gameObject);
  121. if (_Timers == null)
  122. {
  123. _Timers = new List<Timer>();
  124. }
  125. base.Awake();
  126. }
  127. /// <summary>
  128. /// 创建一个简单的计时器
  129. /// </summary>
  130. /// <param name="callBack">回调函数</param>
  131. /// <param name="time">计时器时间</param>
  132. /// <param name="repeats">回调次数 小于0代表循环 大于0代表repeats次</param>
  133. public Timer CreateTimer(TimerHandler callBack, float time, int repeats = 1)
  134. {
  135. return Create(callBack, null, time, repeats);
  136. }
  137. public Timer CreateTimer(TimerArgsHandler callBack, float time, int repeats, params object[] args)
  138. {
  139. return Create(null, callBack, time, repeats, args);
  140. }
  141. private Timer Create(TimerHandler callBack, TimerArgsHandler callBackArgs, float time, int repeats, params object[] args)
  142. {
  143. Timer timer = new Timer(callBack, callBackArgs, time, repeats, args);
  144. _Timers.Add(timer);
  145. return timer;
  146. }
  147. public Timer DestroyTimer(Timer timer)
  148. {
  149. if (timer != null)
  150. {
  151. _Timers.Remove(timer);
  152. timer.CleanUp();
  153. timer = null;
  154. }
  155. return timer;
  156. }
  157. /// <summary>
  158. /// 禁止调用此函数
  159. /// </summary>
  160. public void ClearAll()
  161. {
  162. if (_Timers != null)
  163. {
  164. for (int i = 0; i < _Timers.Count; i++)
  165. {
  166. _Timers[i].CleanUp();
  167. }
  168. _Timers.Clear();
  169. }
  170. }
  171. /// <summary>
  172. /// 固定更新检查更新的频率
  173. /// </summary>
  174. private void Update()
  175. {
  176. if (_Timers.Count != 0)
  177. {
  178. for (int i = _Timers.Count - 1; i >= 0 && i < _Timers.Count; i--)
  179. {
  180. _Timers[i].Update(Time.time);
  181. }
  182. }
  183. }
  184. //private void OnEnable()
  185. //{
  186. // ScenceMain.MainUpdateEvent += MainUpdate;
  187. //}
  188. //private void OnDisable()
  189. //{
  190. // ScenceMain.MainUpdateEvent -= MainUpdate;
  191. //}
  192. }
  193. }