EventComponent.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. namespace WS
  4. {
  5. [ObjectSystem]
  6. public class EventComponentAwakeSystem : AwakeSystem<EventComponent>
  7. {
  8. public override void Awake(EventComponent self)
  9. {
  10. EventComponent.Instance = self;
  11. }
  12. }
  13. ///<summary>全局事件</summary>
  14. public class EventComponent : WSComponent
  15. {
  16. public static EventComponent Instance;
  17. ///<summary>事件代理</summary>
  18. private class Registerations<T> : WSComponent
  19. {
  20. /// <summary>委托可以一对多注册</summary>
  21. public Action<T> OnReceives = obj => { };
  22. public override void Dispose()
  23. {
  24. base.Dispose();
  25. OnReceives = null;
  26. }
  27. }
  28. /// <summary>Type事件类型容器</summary>
  29. private Dictionary<Type, WSComponent> TypeEventDict = new Dictionary<Type, WSComponent>();
  30. /// <summary>Enum事件类型容器</summary>
  31. private Dictionary<string, List<Action<Variable[]>>> EnumEventDic = new Dictionary<string, List<Action<Variable[]>>>();
  32. /// <summary>添加事件监听</summary>
  33. public void AddEvent<T>(Action<T> onReceive)
  34. {
  35. var type = typeof(T);
  36. WSComponent registerations = null;
  37. if (TypeEventDict.TryGetValue(type, out registerations))
  38. {
  39. var reg = (Registerations<T>)registerations;
  40. reg.OnReceives += onReceive;
  41. }
  42. else
  43. {
  44. var reg = new Registerations<T>();
  45. reg.OnReceives += onReceive;
  46. TypeEventDict.Add(type, reg);
  47. }
  48. }
  49. ///<summary>添加事件监听</summary>
  50. public void AddEvent(string eventType, Action<Variable[]> action)
  51. {
  52. if (!EnumEventDic.ContainsKey(eventType))
  53. {
  54. List<Action<Variable[]>> actions = new List<Action<Variable[]>>();
  55. actions.Add(action);
  56. EnumEventDic.Add(eventType, actions);
  57. }
  58. else
  59. {
  60. List<Action<Variable[]>> actions = EnumEventDic[eventType];
  61. if (!actions.Contains(action))
  62. {
  63. actions.Add(action);
  64. EnumEventDic[eventType] = actions;
  65. }
  66. }
  67. }
  68. ///<summary>移除事件监听</summary>
  69. public void RemoveEvent(string eventType, Action<Variable[]> action)
  70. {
  71. if (EnumEventDic.ContainsKey(eventType))
  72. {
  73. if (EnumEventDic[eventType].Contains(action))
  74. {
  75. EnumEventDic[eventType].Remove(action);
  76. if (EnumEventDic[eventType].Count == 0)
  77. {
  78. EnumEventDic.Remove(eventType);
  79. }
  80. }
  81. }
  82. }
  83. /// <summary>移除事件监听</summary>
  84. public void RemoveEvent<T>(Action<T> onReceive)
  85. {
  86. var type = typeof(T);
  87. WSComponent registerations = null;
  88. if (TypeEventDict.TryGetValue(type, out registerations))
  89. {
  90. var reg = (Registerations<T>)registerations;
  91. reg.OnReceives -= onReceive;//将该方法从委托中移除
  92. }
  93. }
  94. /// <summary>发送事件</summary>
  95. public void SendEvent<T>() where T : new()
  96. {
  97. try
  98. {
  99. var type = typeof(T);
  100. WSComponent registerations = null;
  101. if (TypeEventDict.TryGetValue(type, out registerations))
  102. {
  103. var reg = (Registerations<T>)registerations;
  104. reg.OnReceives(new T());//执行委托
  105. }
  106. }
  107. catch (Exception e)
  108. {
  109. Log.LogError(e);
  110. }
  111. }
  112. /// <summary>发送事件</summary>
  113. public void SendEvent<T>(T t)
  114. {
  115. try
  116. {
  117. var type = typeof(T);
  118. WSComponent registerations = null;
  119. if (TypeEventDict.TryGetValue(type, out registerations))
  120. {
  121. var reg = (Registerations<T>)registerations;
  122. reg.OnReceives(t);
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. Log.LogError(e);
  128. }
  129. }
  130. ///<summary>派发事件</summary>
  131. public void SendEvent(string eventType, params Variable[] args)
  132. {
  133. try
  134. {
  135. if (EnumEventDic.ContainsKey(eventType))
  136. {
  137. List<Action<Variable[]>> actions = EnumEventDic[eventType];
  138. for (int i = 0; i < actions.Count; i++)
  139. {
  140. actions[i].Invoke(args);
  141. }
  142. }
  143. }
  144. catch (Exception e)
  145. {
  146. Log.LogError(e);
  147. }
  148. }
  149. /// <summary>清空事件</summary>
  150. public void RemoveAllListener()
  151. {
  152. if (EnumEventDic.Count != 0)
  153. {
  154. EnumEventDic.Clear();
  155. }
  156. foreach (var keyValue in TypeEventDict)
  157. {
  158. keyValue.Value.Dispose();
  159. }
  160. TypeEventDict.Clear();
  161. }
  162. public override void Dispose()
  163. {
  164. base.Dispose();
  165. RemoveAllListener();
  166. }
  167. }
  168. }