UniEvent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UniFramework.Event
  6. {
  7. public static class UniEvent
  8. {
  9. private class PostWrapper
  10. {
  11. public int PostFrame;
  12. public int EventID;
  13. public IEventMessage Message;
  14. public void OnRelease()
  15. {
  16. PostFrame = 0;
  17. EventID = 0;
  18. Message = null;
  19. }
  20. }
  21. private static bool _isInitialize = false;
  22. private static GameObject _driver = null;
  23. private static readonly Dictionary<int, LinkedList<Action<IEventMessage>>> _listeners = new Dictionary<int, LinkedList<Action<IEventMessage>>>(1000);
  24. private static readonly List<PostWrapper> _postingList = new List<PostWrapper>(1000);
  25. /// <summary>
  26. /// 初始化事件系统
  27. /// </summary>
  28. public static void Initalize()
  29. {
  30. if (_isInitialize)
  31. throw new Exception($"{nameof(UniEvent)} is initialized !");
  32. if (_isInitialize == false)
  33. {
  34. // 创建驱动器
  35. _isInitialize = true;
  36. _driver = new UnityEngine.GameObject($"[{nameof(UniEvent)}]");
  37. _driver.AddComponent<UniEventDriver>();
  38. UnityEngine.Object.DontDestroyOnLoad(_driver);
  39. UniLogger.Log($"{nameof(UniEvent)} initalize !");
  40. }
  41. }
  42. /// <summary>
  43. /// 销毁事件系统
  44. /// </summary>
  45. public static void Destroy()
  46. {
  47. if (_isInitialize)
  48. {
  49. ClearAll();
  50. _isInitialize = false;
  51. if (_driver != null)
  52. GameObject.Destroy(_driver);
  53. UniLogger.Log($"{nameof(UniEvent)} destroy all !");
  54. }
  55. }
  56. /// <summary>
  57. /// 更新事件系统
  58. /// </summary>
  59. internal static void Update()
  60. {
  61. for (int i = _postingList.Count - 1; i >= 0; i--)
  62. {
  63. var wrapper = _postingList[i];
  64. if (UnityEngine.Time.frameCount > wrapper.PostFrame)
  65. {
  66. SendMessage(wrapper.EventID, wrapper.Message);
  67. _postingList.RemoveAt(i);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 清空所有监听
  73. /// </summary>
  74. public static void ClearAll()
  75. {
  76. foreach (int eventId in _listeners.Keys)
  77. {
  78. _listeners[eventId].Clear();
  79. }
  80. _listeners.Clear();
  81. _postingList.Clear();
  82. }
  83. /// <summary>
  84. /// 添加监听
  85. /// </summary>
  86. public static void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
  87. {
  88. System.Type eventType = typeof(TEvent);
  89. int eventId = eventType.GetHashCode();
  90. AddListener(eventId, listener);
  91. }
  92. /// <summary>
  93. /// 添加监听
  94. /// </summary>
  95. public static void AddListener(System.Type eventType, System.Action<IEventMessage> listener)
  96. {
  97. int eventId = eventType.GetHashCode();
  98. AddListener(eventId, listener);
  99. }
  100. /// <summary>
  101. /// 添加监听
  102. /// </summary>
  103. public static void AddListener(int eventId, System.Action<IEventMessage> listener)
  104. {
  105. if (_listeners.ContainsKey(eventId) == false)
  106. _listeners.Add(eventId, new LinkedList<Action<IEventMessage>>());
  107. if (_listeners[eventId].Contains(listener) == false)
  108. _listeners[eventId].AddLast(listener);
  109. }
  110. /// <summary>
  111. /// 移除监听
  112. /// </summary>
  113. public static void RemoveListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
  114. {
  115. System.Type eventType = typeof(TEvent);
  116. int eventId = eventType.GetHashCode();
  117. RemoveListener(eventId, listener);
  118. }
  119. /// <summary>
  120. /// 移除监听
  121. /// </summary>
  122. public static void RemoveListener(System.Type eventType, System.Action<IEventMessage> listener)
  123. {
  124. int eventId = eventType.GetHashCode();
  125. RemoveListener(eventId, listener);
  126. }
  127. /// <summary>
  128. /// 移除监听
  129. /// </summary>
  130. public static void RemoveListener(int eventId, System.Action<IEventMessage> listener)
  131. {
  132. if (_listeners.ContainsKey(eventId))
  133. {
  134. if (_listeners[eventId].Contains(listener))
  135. _listeners[eventId].Remove(listener);
  136. }
  137. }
  138. /// <summary>
  139. /// 实时广播事件
  140. /// </summary>
  141. public static void SendMessage(IEventMessage message)
  142. {
  143. int eventId = message.GetType().GetHashCode();
  144. SendMessage(eventId, message);
  145. }
  146. /// <summary>
  147. /// 实时广播事件
  148. /// </summary>
  149. public static void SendMessage(int eventId, IEventMessage message)
  150. {
  151. if (_listeners.ContainsKey(eventId) == false)
  152. return;
  153. LinkedList<Action<IEventMessage>> listeners = _listeners[eventId];
  154. if (listeners.Count > 0)
  155. {
  156. var currentNode = listeners.Last;
  157. while (currentNode != null)
  158. {
  159. currentNode.Value.Invoke(message);
  160. currentNode = currentNode.Previous;
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// 延迟广播事件
  166. /// </summary>
  167. public static void PostMessage(IEventMessage message)
  168. {
  169. int eventId = message.GetType().GetHashCode();
  170. PostMessage(eventId, message);
  171. }
  172. /// <summary>
  173. /// 延迟广播事件
  174. /// </summary>
  175. public static void PostMessage(int eventId, IEventMessage message)
  176. {
  177. var wrapper = new PostWrapper();
  178. wrapper.PostFrame = UnityEngine.Time.frameCount;
  179. wrapper.EventID = eventId;
  180. wrapper.Message = message;
  181. _postingList.Add(wrapper);
  182. }
  183. }
  184. }