Timers.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections.Generic;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. public delegate void TimerCallback(object param);
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class Timers
  11. {
  12. public static int repeat;
  13. public static float time;
  14. public static bool catchCallbackExceptions = false;
  15. Dictionary<TimerCallback, Anymous_T> _items;
  16. Dictionary<TimerCallback, Anymous_T> _toAdd;
  17. List<Anymous_T> _toRemove;
  18. List<Anymous_T> _pool;
  19. TimersEngine _engine;
  20. GameObject gameObject;
  21. private static Timers _inst;
  22. public static Timers inst
  23. {
  24. get
  25. {
  26. if (_inst == null)
  27. _inst = new Timers();
  28. return _inst;
  29. }
  30. }
  31. public Timers()
  32. {
  33. _inst = this;
  34. gameObject = new GameObject("[FairyGUI.Timers]");
  35. gameObject.hideFlags = HideFlags.HideInHierarchy;
  36. gameObject.SetActive(true);
  37. Object.DontDestroyOnLoad(gameObject);
  38. _engine = gameObject.AddComponent<TimersEngine>();
  39. _items = new Dictionary<TimerCallback, Anymous_T>();
  40. _toAdd = new Dictionary<TimerCallback, Anymous_T>();
  41. _toRemove = new List<Anymous_T>();
  42. _pool = new List<Anymous_T>(100);
  43. }
  44. public void Add(float interval, int repeat, TimerCallback callback)
  45. {
  46. Add(interval, repeat, callback, null);
  47. }
  48. /**
  49. * @interval in seconds
  50. * @repeat 0 indicate loop infinitely, otherwise the run count
  51. **/
  52. public void Add(float interval, int repeat, TimerCallback callback, object callbackParam)
  53. {
  54. if (callback == null)
  55. {
  56. Debug.LogWarning("timer callback is null, " + interval + "," + repeat);
  57. return;
  58. }
  59. Anymous_T t;
  60. if (_items.TryGetValue(callback, out t))
  61. {
  62. t.set(interval, repeat, callback, callbackParam);
  63. t.elapsed = 0;
  64. t.deleted = false;
  65. return;
  66. }
  67. if (_toAdd.TryGetValue(callback, out t))
  68. {
  69. t.set(interval, repeat, callback, callbackParam);
  70. return;
  71. }
  72. t = GetFromPool();
  73. t.interval = interval;
  74. t.repeat = repeat;
  75. t.callback = callback;
  76. t.param = callbackParam;
  77. _toAdd[callback] = t;
  78. }
  79. public void CallLater(TimerCallback callback)
  80. {
  81. Add(0.001f, 1, callback);
  82. }
  83. public void CallLater(TimerCallback callback, object callbackParam)
  84. {
  85. Add(0.001f, 1, callback, callbackParam);
  86. }
  87. public void AddUpdate(TimerCallback callback)
  88. {
  89. Add(0.001f, 0, callback);
  90. }
  91. public void AddUpdate(TimerCallback callback, object callbackParam)
  92. {
  93. Add(0.001f, 0, callback, callbackParam);
  94. }
  95. public void StartCoroutine(IEnumerator routine)
  96. {
  97. _engine.StartCoroutine(routine);
  98. }
  99. public bool Exists(TimerCallback callback)
  100. {
  101. if (_toAdd.ContainsKey(callback))
  102. return true;
  103. Anymous_T at;
  104. if (_items.TryGetValue(callback, out at))
  105. return !at.deleted;
  106. return false;
  107. }
  108. public void Remove(TimerCallback callback)
  109. {
  110. Anymous_T t;
  111. if (_toAdd.TryGetValue(callback, out t))
  112. {
  113. _toAdd.Remove(callback);
  114. ReturnToPool(t);
  115. }
  116. if (_items.TryGetValue(callback, out t))
  117. t.deleted = true;
  118. }
  119. private Anymous_T GetFromPool()
  120. {
  121. Anymous_T t;
  122. int cnt = _pool.Count;
  123. if (cnt > 0)
  124. {
  125. t = _pool[cnt - 1];
  126. _pool.RemoveAt(cnt - 1);
  127. t.deleted = false;
  128. t.elapsed = 0;
  129. }
  130. else
  131. t = new Anymous_T();
  132. return t;
  133. }
  134. private void ReturnToPool(Anymous_T t)
  135. {
  136. t.callback = null;
  137. _pool.Add(t);
  138. }
  139. public void Update()
  140. {
  141. float dt = Time.unscaledDeltaTime;
  142. Dictionary<TimerCallback, Anymous_T>.Enumerator iter;
  143. if (_items.Count > 0)
  144. {
  145. iter = _items.GetEnumerator();
  146. while (iter.MoveNext())
  147. {
  148. Anymous_T i = iter.Current.Value;
  149. if (i.deleted)
  150. {
  151. _toRemove.Add(i);
  152. continue;
  153. }
  154. i.elapsed += dt;
  155. if (i.elapsed < i.interval)
  156. continue;
  157. i.elapsed -= i.interval;
  158. if (i.elapsed < 0 || i.elapsed > 0.03f)
  159. i.elapsed = 0;
  160. if (i.repeat > 0)
  161. {
  162. i.repeat--;
  163. if (i.repeat == 0)
  164. {
  165. i.deleted = true;
  166. _toRemove.Add(i);
  167. }
  168. }
  169. repeat = i.repeat;
  170. if (i.callback != null)
  171. {
  172. if (catchCallbackExceptions)
  173. {
  174. try
  175. {
  176. i.callback(i.param);
  177. }
  178. catch (System.Exception e)
  179. {
  180. i.deleted = true;
  181. Debug.LogWarning("FairyGUI: timer(internal=" + i.interval + ", repeat=" + i.repeat + ") callback error > " + e.Message);
  182. }
  183. }
  184. else
  185. i.callback(i.param);
  186. }
  187. }
  188. iter.Dispose();
  189. }
  190. int len = _toRemove.Count;
  191. if (len > 0)
  192. {
  193. for (int k = 0; k < len; k++)
  194. {
  195. Anymous_T i = _toRemove[k];
  196. if (i.deleted && i.callback != null)
  197. {
  198. _items.Remove(i.callback);
  199. ReturnToPool(i);
  200. }
  201. }
  202. _toRemove.Clear();
  203. }
  204. if (_toAdd.Count > 0)
  205. {
  206. iter = _toAdd.GetEnumerator();
  207. while (iter.MoveNext())
  208. _items.Add(iter.Current.Key, iter.Current.Value);
  209. iter.Dispose();
  210. _toAdd.Clear();
  211. }
  212. }
  213. }
  214. class Anymous_T
  215. {
  216. public float interval;
  217. public int repeat;
  218. public TimerCallback callback;
  219. public object param;
  220. public float elapsed;
  221. public bool deleted;
  222. public void set(float interval, int repeat, TimerCallback callback, object param)
  223. {
  224. this.interval = interval;
  225. this.repeat = repeat;
  226. this.callback = callback;
  227. this.param = param;
  228. }
  229. }
  230. class TimersEngine : MonoBehaviour
  231. {
  232. void Update()
  233. {
  234. Timers.inst.Update();
  235. }
  236. }
  237. }