Timer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /****************************************************************************
  2. * Copyright (c) 2017 snowcold
  3. * Copyright (c) 2017 liangxie
  4. * https://github.com/akbiggs/UnityTimer
  5. ****************************************************************************/
  6. using System;
  7. using UnityEngine;
  8. namespace QFramework
  9. {
  10. [MonoSingletonPath("[Tools]/Timer")]
  11. public class Timer : MonoBehaviour,ISingleton
  12. {
  13. public static Timer Instance => MonoSingletonProperty<Timer>.Instance;
  14. private readonly BinaryHeap<TimeItem> m_UnScaleTimeHeap = new BinaryHeap<TimeItem>(128, BinaryHeapSortMode.kMin);
  15. private readonly BinaryHeap<TimeItem> m_ScaleTimeHeap = new BinaryHeap<TimeItem>(128, BinaryHeapSortMode.kMin);
  16. private float m_CurrentUnScaleTime = -1;
  17. private float m_CurrentScaleTime = -1;
  18. public float currentScaleTime
  19. {
  20. get { return m_CurrentScaleTime; }
  21. }
  22. public float currentUnScaleTime
  23. {
  24. get { return m_CurrentUnScaleTime; }
  25. }
  26. public void OnSingletonInit()
  27. {
  28. m_UnScaleTimeHeap.Clear();
  29. m_ScaleTimeHeap.Clear();
  30. m_CurrentUnScaleTime = Time.unscaledTime;
  31. m_CurrentScaleTime = Time.time;
  32. }
  33. public void ResetMgr()
  34. {
  35. m_UnScaleTimeHeap.Clear();
  36. m_ScaleTimeHeap.Clear();
  37. }
  38. public void StartMgr()
  39. {
  40. m_CurrentUnScaleTime = Time.unscaledTime;
  41. m_CurrentScaleTime = Time.time;
  42. }
  43. #region 投递受缩放影响定时器
  44. public TimeItem Post2Scale(Action<int> callback, float delay, int repeat)
  45. {
  46. TimeItem item = TimeItem.Allocate(callback, delay, repeat);
  47. Post2Scale(item);
  48. return item;
  49. }
  50. public TimeItem Post2Scale(Action<int> callback, float delay)
  51. {
  52. TimeItem item = TimeItem.Allocate(callback, delay);
  53. Post2Scale(item);
  54. return item;
  55. }
  56. public void Post2Scale(TimeItem item)
  57. {
  58. item.SortScore = m_CurrentScaleTime + item.DelayTime();
  59. m_ScaleTimeHeap.Insert(item);
  60. }
  61. #endregion
  62. #region 投递真实时间定时器
  63. //投递指定时间计时器:只支持标准时间
  64. public TimeItem Post2Really(Action<int> callback, DateTime toTime)
  65. {
  66. float passTick = (toTime.Ticks - DateTime.Now.Ticks) / 10000000;
  67. if (passTick < 0)
  68. {
  69. Debug.LogWarning("Timer Set Pass Time...");
  70. passTick = 0;
  71. }
  72. return Post2Really(callback, passTick);
  73. }
  74. public TimeItem Post2Really(Action<int> callback, float delay, int repeat)
  75. {
  76. TimeItem item = TimeItem.Allocate(callback, delay, repeat);
  77. Post2Really(item);
  78. return item;
  79. }
  80. public TimeItem Post2Really(Action<int> callback, float delay)
  81. {
  82. TimeItem item = TimeItem.Allocate(callback, delay);
  83. Post2Really(item);
  84. return item;
  85. }
  86. public void Post2Really(TimeItem item)
  87. {
  88. item.SortScore = m_CurrentUnScaleTime + item.DelayTime();
  89. m_UnScaleTimeHeap.Insert(item);
  90. }
  91. #endregion
  92. public void Update()
  93. {
  94. UpdateMgr();
  95. }
  96. public void UpdateMgr()
  97. {
  98. TimeItem item = null;
  99. m_CurrentUnScaleTime = Time.unscaledTime;
  100. m_CurrentScaleTime = Time.time;
  101. #region 不受缩放影响定时器更新
  102. while ((item = m_UnScaleTimeHeap.Top()) != null)
  103. {
  104. if (!item.isEnable)
  105. {
  106. m_UnScaleTimeHeap.Pop();
  107. item.Recycle2Cache();
  108. continue;
  109. }
  110. if (item.SortScore < m_CurrentUnScaleTime)
  111. {
  112. m_UnScaleTimeHeap.Pop();
  113. item.OnTimeTick();
  114. if (item.isEnable && item.NeedRepeat())
  115. {
  116. Post2Really(item);
  117. }
  118. else
  119. {
  120. item.Recycle2Cache();
  121. }
  122. }
  123. else
  124. {
  125. break;
  126. }
  127. }
  128. #endregion
  129. #region 受缩放影响定时器更新
  130. while ((item = m_ScaleTimeHeap.Top()) != null)
  131. {
  132. if (!item.isEnable)
  133. {
  134. m_ScaleTimeHeap.Pop();
  135. item.Recycle2Cache();
  136. continue;
  137. }
  138. if (item.SortScore < m_CurrentScaleTime)
  139. {
  140. m_ScaleTimeHeap.Pop();
  141. item.OnTimeTick();
  142. if (item.isEnable && item.NeedRepeat())
  143. {
  144. Post2Scale(item);
  145. }
  146. else
  147. {
  148. item.Recycle2Cache();
  149. }
  150. }
  151. else
  152. {
  153. break;
  154. }
  155. }
  156. #endregion
  157. }
  158. public void Dump()
  159. {
  160. }
  161. }
  162. }