TimerMgr.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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, System.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. if (Repeats == 0)
  43. {
  44. OnFinsh?.Invoke();
  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. OnDestroy?.Invoke();
  57. OnFinsh?.Invoke();
  58. OnDestroy = null;
  59. OnComplete = null;
  60. }
  61. }
  62. /// <summary>
  63. /// 计时器
  64. /// 添加一个计时事件
  65. /// 删除一个计时事件
  66. /// 更新计时事件
  67. /// </summary>
  68. public class TimerMgr : UnitySingleton<TimerMgr>
  69. {
  70. private List<Timer> _Timers;//时间管理器
  71. protected override void Awake()
  72. {
  73. DontDestroyOnLoad(gameObject);
  74. if (_Timers == null)
  75. {
  76. _Timers = new List<Timer>();
  77. }
  78. base.Awake();
  79. }
  80. /// <summary>
  81. /// 创建一个简单的计时器
  82. /// </summary>
  83. /// <param name="callBack">回调函数</param>
  84. /// <param name="time">计时器时间</param>
  85. /// <param name="repeats">回调次数 小于0代表循环 大于0代表repeats次</param>
  86. public Timer CreateTimer(TimerHandler callBack, float time, int repeats = 1)
  87. {
  88. return Create(callBack, null, time, repeats);
  89. }
  90. public Timer CreateTimer(TimerArgsHandler callBack, float time, int repeats, params System.Object[] args)
  91. {
  92. return Create(null, callBack, time, repeats, args);
  93. }
  94. private Timer Create(TimerHandler callBack, TimerArgsHandler callBackArgs, float time, int repeats, params System.Object[] args)
  95. {
  96. Timer timer = new Timer(callBack, callBackArgs, time, repeats, args);
  97. _Timers.Add(timer);
  98. return timer;
  99. }
  100. public Timer DestroyTimer(Timer timer)
  101. {
  102. if (timer != null)
  103. {
  104. _Timers.Remove(timer);
  105. timer.CleanUp();
  106. timer = null;
  107. }
  108. return timer;
  109. }
  110. public void ClearAll()
  111. {
  112. if (_Timers != null)
  113. {
  114. for (int i = 0; i < _Timers.Count; i++)
  115. {
  116. _Timers[i].CleanUp();
  117. }
  118. _Timers.Clear();
  119. }
  120. }
  121. /// <summary>
  122. /// 固定更新检查更新的频率
  123. /// </summary>
  124. void Update()
  125. {
  126. if (_Timers.Count != 0)
  127. {
  128. for (int i = _Timers.Count - 1; i >= 0 && i < _Timers.Count; i--)
  129. {
  130. Timer timer = _Timers[i];
  131. float curTime = Time.time;
  132. if (timer.Frequency + timer.LastTickTime > curTime)
  133. {
  134. continue;
  135. }
  136. timer.LastTickTime = curTime;
  137. timer?.Notify();
  138. timer.Repeats--;
  139. //计时完成,可以删除了
  140. if (timer.Repeats == 0)
  141. {
  142. DestroyTimer(timer);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }