TimerMgr.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(System.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 System.Object[] Args;
  15. public float LastTickTime;
  16. public event Action OnComplete; //计时器完成一次工作
  17. public event Action OnDestroy; //计时器被销毁
  18. public Timer() { }
  19. /// <summary>
  20. /// 创建一个时间事件对象
  21. /// </summary>
  22. /// <param name="Handler">回调函数</param>
  23. /// <param name="ArgsHandler">带参数的回调函数</param>
  24. /// <param name="frequency">时间内执行</param>
  25. /// <param name="repeats">重复次数</param>
  26. /// <param name="Args">参数 可以任意的传不定数量,类型的参数</param>
  27. public Timer(TimerHandler Handler, TimerArgsHandler ArgsHandler, float frequency, int repeats, System.Object[] Args)
  28. {
  29. this.Handler = Handler;
  30. this.ArgsHandler = ArgsHandler;
  31. this.Frequency = frequency;
  32. this.Repeats = repeats == 0 ? 1 : repeats;
  33. this.Args = Args;
  34. this.LastTickTime = Time.time;
  35. }
  36. public void Notify()
  37. {
  38. if (Handler != null)
  39. Handler();
  40. if (ArgsHandler != null)
  41. ArgsHandler(Args);
  42. if (OnComplete != null)
  43. {
  44. OnComplete();
  45. }
  46. }
  47. /// <summary>
  48. /// 清理计时器,初始化参数 同时清理事件
  49. /// </summary>
  50. public void CleanUp()
  51. {
  52. Handler = null;
  53. ArgsHandler = null;
  54. Repeats = 1;
  55. Frequency = 0;
  56. if (OnDestroy != null)
  57. {
  58. OnDestroy();
  59. }
  60. OnDestroy = null;
  61. OnComplete = null;
  62. }
  63. }
  64. /// <summary>
  65. /// 计时器
  66. /// 添加一个计时事件
  67. /// 删除一个计时事件
  68. /// 更新计时事件
  69. /// </summary>
  70. public class TimerMgr : UnitySingleton<TimerMgr>
  71. {
  72. private List<Timer> _Timers;//时间管理器
  73. protected override void Awake()
  74. {
  75. DontDestroyOnLoad(gameObject);
  76. if (_Timers == null)
  77. {
  78. _Timers = new List<Timer>();
  79. }
  80. base.Awake();
  81. }
  82. /// <summary>
  83. /// 创建一个简单的计时器
  84. /// </summary>
  85. /// <param name="callBack">回调函数</param>
  86. /// <param name="time">计时器时间</param>
  87. /// <param name="repeats">回调次数 小于0代表循环 大于0代表repeats次</param>
  88. public Timer CreateTimer(TimerHandler callBack, float time, int repeats = 1)
  89. {
  90. return Create(callBack, null, time, repeats);
  91. }
  92. public Timer CreateTimerWithArgs(TimerArgsHandler callBack, float time, int repeats, params System.Object[] args)
  93. {
  94. return Create(null, callBack, time, repeats, args);
  95. }
  96. private Timer Create(TimerHandler callBack, TimerArgsHandler callBackArgs, float time, int repeats, params System.Object[] args)
  97. {
  98. Timer timer = new Timer(callBack, callBackArgs, time, repeats, args);
  99. _Timers.Add(timer);
  100. return timer;
  101. }
  102. public Timer DestroyTimer(Timer timer)
  103. {
  104. if (timer != null)
  105. {
  106. _Timers.Remove(timer);
  107. timer.CleanUp();
  108. timer = null;
  109. }
  110. return timer;
  111. }
  112. public void ClearAll()
  113. {
  114. if (_Timers != null)
  115. {
  116. for (int i = 0; i < _Timers.Count; i++)
  117. {
  118. _Timers[i].CleanUp();
  119. }
  120. _Timers.Clear();
  121. }
  122. }
  123. /// <summary>
  124. /// 固定更新检查更新的频率
  125. /// </summary>
  126. void Update()
  127. {
  128. if (_Timers.Count != 0)
  129. {
  130. for (int i = _Timers.Count - 1; i >= 0 && i < _Timers.Count; i--)
  131. {
  132. Timer timer = _Timers[i];
  133. float curTime = Time.time;
  134. if (timer.Frequency + timer.LastTickTime > curTime)
  135. {
  136. continue;
  137. }
  138. timer.LastTickTime = curTime;
  139. if (timer.Repeats-- == 0)
  140. {//计时完成,可以删除了
  141. DestroyTimer(timer);
  142. }
  143. else
  144. {//触发计时
  145. timer.Notify();
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }