当前位置:   article > 正文

U3D游戏开发框架(八)——消息系统_unity 消息系统

unity 消息系统

一:目的

为什么要使用消息系统呢?消息系统的作用就是解耦合
消息系统就例如我们约好几个小伙伴出去玩,但是现在临时有事取消计划,如果没有群聊那么你需要挨个通知每一个人,但是有了群聊,只要在群里发一条消息就能通知到大家,前提是大家都进了这个群,才能收到这个消息。每个人进群就相当于绑定监听消息的方法,退群就相当于移除监听消息的方法,你在群里发一条消息就相当于发送消息的方法

Unity提供了一种消息机制:SendMessage,但它实现的是一种伪监听者模式,利用的是反射机制,性能较低,几乎不会有人在项目中使用SendMessage,一般都是去实现一套自己的消息系统


二:解决的问题及优点

——解耦合,使模块与模块之间解耦,与消息中心耦合


三:使用方法

——MsgConst中添加事件常量
——在类初始化和销毁时分别注册和注销事件
——发送事件使用MsgSystem.Dispatch

  1. using UnityEngine;
  2. public class : MonoBehaviour
  3. {
  4. private void Awake()
  5. {
  6. MsgSystem.AddListener(MsgConst.Test, Test_NoParam);
  7. MsgSystem.AddListener<int>(MsgConst.Test, Test_OneParam);
  8. MsgSystem.AddListener<int, long>(MsgConst.Test, Test_TwoParam);
  9. }
  10. void Test_NoParam()
  11. {
  12. Debug.Log("no param");
  13. }
  14. void Test_OneParam(int i)
  15. {
  16. Debug.Log("one param");
  17. }
  18. void Test_TwoParam(int i, long j)
  19. {
  20. Debug.Log("two param");
  21. }
  22. private void OnDestroy()
  23. {
  24. MsgSystem.RemoveListener(MsgConst.Test, Test_NoParam);
  25. MsgSystem.RemoveListener<int>(MsgConst.Test, Test_OneParam);
  26. MsgSystem.RemoveListener<int, long>(MsgConst.Test, Test_TwoParam);
  27. }
  28. }

四:代码实现

  1. using System.Collections.Generic;
  2. using System;
  3. using System.Text;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 事件系统
  7. /// </summary>
  8. public static class MsgSystem
  9. {
  10. //存所有事件的字典
  11. static Dictionary<string, List<Delegate>> m_EventDict = new Dictionary<string, List<Delegate>>();
  12. /// <summary>
  13. /// 添加监听
  14. /// </summary>
  15. static void AddListener(string key, Delegate callBack)
  16. {
  17. List<Delegate> eventList;
  18. if (m_EventDict.TryGetValue(key, out eventList))
  19. {
  20. eventList.Add(callBack);
  21. }
  22. else
  23. {
  24. eventList = new List<Delegate>();
  25. eventList.Add(callBack);
  26. m_EventDict.Add(key, eventList);
  27. }
  28. }
  29. /// <summary>
  30. /// 移除监听
  31. /// </summary>
  32. static void RemoveListener(string key, Delegate callBack)
  33. {
  34. List<Delegate> eventList;
  35. if (m_EventDict.TryGetValue(key, out eventList))
  36. {
  37. eventList.Remove(callBack);
  38. }
  39. if (eventList.Count == 0)
  40. {
  41. m_EventDict.Remove(key);
  42. }
  43. }
  44. /// <summary>
  45. /// 移除所有监听
  46. /// </summary>
  47. public static void RemoveAllListener()
  48. {
  49. m_EventDict.Clear();
  50. }
  51. #region 添加监听
  52. public static void AddListener(string key, Action callBack)
  53. {
  54. AddListener(key, (Delegate)callBack);
  55. }
  56. public static void AddListener<T1>(string key, Action<T1> callBack)
  57. {
  58. AddListener(key, (Delegate)callBack);
  59. }
  60. public static void AddListener<T1, T2>(string key, Action<T1, T2> callBack)
  61. {
  62. AddListener(key, (Delegate)callBack);
  63. }
  64. public static void AddListener<T1, T2, T3>(string key, Action<T1, T2, T3> callBack)
  65. {
  66. AddListener(key, (Delegate)callBack);
  67. }
  68. public static void AddListener<T1, T2, T3, T4>(string key, Action<T1, T2, T3, T4> callBack)
  69. {
  70. AddListener(key, (Delegate)callBack);
  71. }
  72. public static void AddListener<T1, T2, T3, T4, T5>(string key, Action<T1, T2, T3, T4, T5> callBack)
  73. {
  74. AddListener(key, (Delegate)callBack);
  75. }
  76. #endregion
  77. #region 移除监听
  78. public static void RemoveListener(string key, Action callBack)
  79. {
  80. RemoveListener(key, (Delegate)callBack);
  81. }
  82. public static void RemoveListener<T1>(string key, Action<T1> callBack)
  83. {
  84. RemoveListener(key, (Delegate)callBack);
  85. }
  86. public static void RemoveListener<T1, T2>(string key, Action<T1, T2> callBack)
  87. {
  88. RemoveListener(key, (Delegate)callBack);
  89. }
  90. public static void RemoveListener<T1, T2, T3>(string key, Action<T1, T2, T3> callBack)
  91. {
  92. RemoveListener(key, (Delegate)callBack);
  93. }
  94. public static void RemoveListener<T1, T2, T3, T4>(string key, Action<T1, T2, T3, T4> callBack)
  95. {
  96. RemoveListener(key, (Delegate)callBack);
  97. }
  98. public static void RemoveListener<T1, T2, T3, T4, T5>(string key, Action<T1, T2, T3, T4, T5> callBack)
  99. {
  100. RemoveListener(key, (Delegate)callBack);
  101. }
  102. #endregion
  103. #region 分发消息
  104. public static void Dispatch(string key)
  105. {
  106. List<Delegate> eventList;
  107. if (m_EventDict.TryGetValue(key, out eventList))
  108. {
  109. for (int i = 0; i < eventList.Count; i++)
  110. {
  111. if (eventList[i] is Action)
  112. {
  113. Action callBack = eventList[i] as Action;
  114. callBack?.Invoke();
  115. }
  116. }
  117. }
  118. }
  119. public static void Dispatch<T1>(string key, T1 arg1)
  120. {
  121. List<Delegate> eventList;
  122. if (m_EventDict.TryGetValue(key, out eventList))
  123. {
  124. for (int i = 0; i < eventList.Count; i++)
  125. {
  126. if (eventList[i] is Action<T1>)
  127. {
  128. Action<T1> callBack = eventList[i] as Action<T1>;
  129. callBack?.Invoke(arg1);
  130. }
  131. }
  132. }
  133. }
  134. public static void Dispatch<T1, T2>(string key, T1 arg1, T2 arg2)
  135. {
  136. List<Delegate> eventList;
  137. if (m_EventDict.TryGetValue(key, out eventList))
  138. {
  139. for (int i = 0; i < eventList.Count; i++)
  140. {
  141. if (eventList[i] is Action<T1, T2>)
  142. {
  143. Action<T1, T2> callBack = eventList[i] as Action<T1, T2>;
  144. callBack?.Invoke(arg1, arg2);
  145. }
  146. }
  147. }
  148. }
  149. public static void Dispatch<T1, T2, T3>(string key, T1 arg1, T2 arg2, T3 arg3)
  150. {
  151. List<Delegate> eventList;
  152. if (m_EventDict.TryGetValue(key, out eventList))
  153. {
  154. for (int i = 0; i < eventList.Count; i++)
  155. {
  156. if (eventList[i] is Action<T1, T2, T3>)
  157. {
  158. Action<T1, T2, T3> callBack = eventList[i] as Action<T1, T2, T3>;
  159. callBack?.Invoke(arg1, arg2, arg3);
  160. }
  161. }
  162. }
  163. }
  164. public static void Dispatch<T1, T2, T3, T4>(string key, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  165. {
  166. List<Delegate> eventList;
  167. if (m_EventDict.TryGetValue(key, out eventList))
  168. {
  169. for (int i = 0; i < eventList.Count; i++)
  170. {
  171. if (eventList[i] is Action<T1, T2, T3, T4>)
  172. {
  173. Action<T1, T2, T3, T4> callBack = eventList[i] as Action<T1, T2, T3, T4>;
  174. callBack?.Invoke(arg1, arg2, arg3, arg4);
  175. }
  176. }
  177. }
  178. }
  179. public static void Dispatch<T1, T2, T3, T4, T5>(string key, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
  180. {
  181. List<Delegate> eventList;
  182. if (m_EventDict.TryGetValue(key, out eventList))
  183. {
  184. for (int i = 0; i < eventList.Count; i++)
  185. {
  186. if (eventList[i] is Action<T1, T2, T3, T4, T5>)
  187. {
  188. Action<T1, T2, T3, T4, T5> callBack = eventList[i] as Action<T1, T2, T3, T4, T5>;
  189. callBack?.Invoke(arg1, arg2, arg3, arg4, arg5);
  190. }
  191. }
  192. }
  193. }
  194. #endregion
  195. public static void Log()
  196. {
  197. var sb = new StringBuilder();
  198. sb.Append("----------输出所有注册事件------------");
  199. sb.Append("\n");
  200. foreach (var pair in m_EventDict)
  201. {
  202. sb.AppendFormat($"key = {pair.Key}");
  203. sb.Append("\n");
  204. sb.AppendFormat($"event = {pair.Value.Count}");
  205. sb.Append("\n");
  206. foreach (var callback in pair.Value)
  207. {
  208. sb.AppendFormat($"cs : {callback.Method.DeclaringType} , event name : {callback.Method.Name}");
  209. sb.Append("\n");
  210. }
  211. sb.Append("\n");
  212. }
  213. sb.Append("\n");
  214. sb.Append("---------------------------------------");
  215. Debug.Log(sb);
  216. }
  217. }
  1. /// <summary>
  2. /// 事件常量
  3. /// </summary>
  4. public class EventConst
  5. {
  6. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/136053
推荐阅读
相关标签
  

闽ICP备14008679号