TimerMgr.cs 5.1 KB

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