Unity消息简易框架 Advanced C# messenger

Unity C# 消息机制 

【转载 雨凇MOMO博客】 https://www.xuanyusong.com/archives/2165

Tips

在寻找Unity下好用的消息事件框架的时候无意中发现该框架,十分简易,但又用起来特别方便。公司VR游戏中试了下,真滴方便。也不造轮子了,直接转载雨凇大佬的博客,就当是Mark一下,方便以后学习。

正文

Advanced CSharp Messenger 属于C#事件的一种。 维基百科中由详细的说明http://wiki.unity3d.com/index.php?title=Advanced_CSharp_Messenger 上周的一天刚巧有朋友问到我这一块的知识,那么我研究出来将它贴在博客中,帮助了他也帮助我自己!哇咔咔。

Advanced CSharp Messenger的特点可以将游戏对象做为参数发送。到底Advanced CSharp Messenger有什么用呢?先创建一个立方体对象,然后把Script脚本绑定在这个对象中。脚本中有一个方法叫DoSomething()。写一段简单的代码,通常我们在调用方法的时候需要这样来写。

  1. private Script script; 

  2. void Awake() 



  3. GameObject cube = GameObject.Find("Cube"); 

  4. script = cube.GetComponent<Script>(); 




  5. void Update() 



  6. if(Input.GetMouseButtonDown(0)) 



  7. script.DoSomething(); 






代码比较简单,我就不注释了。 原理就是先获取游戏对象,接着获取脚本组件对象,最后通过脚本组件对象去调用对应脚本中的方法,这样的调用方法我们称之为直接调用。

这个例子中我只调用了一个对象的方法,如果说有成千上万个对象,那么这样调用是不是感觉自己的代码非常的丑?因为你需要一个一个的获取对象然后获取脚本组件然后在调用方法。。。。。 (想想都恐怖!!)

下面我们在用Advanced CSharp Messenger来实现事件的调用。按照维基百科中首先把Message.cs 和Callback.cs拷贝在你的工程中。

CallBack.cs

  1. public delegate void Callback(); 

  2. public delegate void Callback<T>(T arg1); 

  3. public delegate void Callback<T, U>(T arg1, U arg2); 

  4. public delegate void Callback<T, U, V>(T arg1, U arg2, V arg3); 

Message.cs

  1. /* 

  2. * Advanced C# messenger by Ilya Suzdalnitski. V1.0 

  3. *  

  4. * Based on Rod Hyde's "CSharpMessenger" and Magnus Wolffelt's "CSharpMessenger Extended". 

  5. *  

  6. * Features: 

  7. * Prevents a MissingReferenceException because of a reference to a destroyed message handler. 

  8. * Option to log all messages 

  9. * Extensive error detection, preventing silent bugs 

  10. *  

  11. * Usage examples: 

  12. 1. Messenger.AddListener<GameObject>("prop collected", PropCollected); 

  13. Messenger.Broadcast<GameObject>("prop collected", prop); 

  14. 2. Messenger.AddListener<float>("speed changed", SpeedChanged); 

  15. Messenger.Broadcast<float>("speed changed", 0.5f); 

  16. *  

  17. * Messenger cleans up its evenTable automatically upon loading of a new level. 

  18. *  

  19. * Don't forget that the messages that should survive the cleanup, should be marked with Messenger.MarkAsPermanent(string) 

  20. *  

  21. */ 


  22. //#define LOG_ALL_MESSAGES 

  23. //#define LOG_ADD_LISTENER 

  24. //#define LOG_BROADCAST_MESSAGE 

  25. #define REQUIRE_LISTENER 


  26. using System; 

  27. using System.Collections.Generic; 

  28. using UnityEngine; 


  29. static internal class Messenger { 

  30. #region Internal variables 


  31. //Disable the unused variable warning 

  32. #pragma warning disable 0414 

  33. //Ensures that the MessengerHelper will be created automatically upon start of the game. 

  34. static private MessengerHelper messengerHelper = ( new GameObject("MessengerHelper") ).AddComponent< MessengerHelper >(); 

  35. #pragma warning restore 0414 


  36. static public Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>(); 


  37. //Message handlers that should never be removed, regardless of calling Cleanup 

  38. static public List< string > permanentMessages = new List< string > (); 

  39. #endregion 

  40. #region Helper methods 

  41. //Marks a certain message as permanent. 

  42. static public void MarkAsPermanent(string eventType) { 

  43. #if LOG_ALL_MESSAGES 

  44. Debug.Log("Messenger MarkAsPermanent \t\"" + eventType + "\""); 

  45. #endif 


  46. permanentMessages.Add( eventType ); 




  47. static public void Cleanup() 



  48. #if LOG_ALL_MESSAGES 

  49. Debug.Log("MESSENGER Cleanup. Make sure that none of necessary listeners are removed."); 

  50. #endif 


  51. List< string > messagesToRemove = new List<string>(); 


  52. foreach (KeyValuePair<string, Delegate> pair in eventTable) { 

  53. bool wasFound = false; 


  54. foreach (string message in permanentMessages) { 

  55. if (pair.Key == message) { 

  56. wasFound = true; 

  57. break; 






  58. if (!wasFound) 

  59. messagesToRemove.Add( pair.Key ); 




  60. foreach (string message in messagesToRemove) { 

  61. eventTable.Remove( message ); 






  62. static public void PrintEventTable() 



  63. Debug.Log("\t\t\t=== MESSENGER PrintEventTable ==="); 


  64. foreach (KeyValuePair<string, Delegate> pair in eventTable) { 

  65. Debug.Log("\t\t\t" + pair.Key + "\t\t" + pair.Value); 




  66. Debug.Log("\n"); 



  67. #endregion 


  68. #region Message logging and exception throwing 

  69. static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded) { 

  70. #if LOG_ALL_MESSAGES || LOG_ADD_LISTENER 

  71. Debug.Log("MESSENGER OnListenerAdding \t\"" + eventType + "\"\t{" + listenerBeingAdded.Target + " -> " + listenerBeingAdded.Method + "}"); 

  72. #endif 


  73. if (!eventTable.ContainsKey(eventType)) { 

  74. eventTable.Add(eventType, null ); 




  75. Delegate d = eventTable[eventType]; 

  76. if (d != null && d.GetType() != listenerBeingAdded.GetType()) { 

  77. throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, d.GetType().Name, listenerBeingAdded.GetType().Name)); 






  78. static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved) { 

  79. #if LOG_ALL_MESSAGES 

  80. Debug.Log("MESSENGER OnListenerRemoving \t\"" + eventType + "\"\t{" + listenerBeingRemoved.Target + " -> " + listenerBeingRemoved.Method + "}"); 

  81. #endif 


  82. if (eventTable.ContainsKey(eventType)) { 

  83. Delegate d = eventTable[eventType]; 


  84. if (d == null) { 

  85. throw new ListenerException(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType)); 

  86. } else if (d.GetType() != listenerBeingRemoved.GetType()) { 

  87. throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, d.GetType().Name, listenerBeingRemoved.GetType().Name)); 



  88. } else { 

  89. throw new ListenerException(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType)); 






  90. static public void OnListenerRemoved(string eventType) { 

  91. if (eventTable[eventType] == null) { 

  92. eventTable.Remove(eventType); 






  93. static public void OnBroadcasting(string eventType) { 

  94. #if REQUIRE_LISTENER 

  95. if (!eventTable.ContainsKey(eventType)) { 

  96. throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but no listener found. Try marking the message with Messenger.MarkAsPermanent.", eventType)); 



  97. #endif 




  98. static public BroadcastException CreateBroadcastSignatureException(string eventType) { 

  99. return new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType)); 




  100. public class BroadcastException : Exception { 

  101. public BroadcastException(string msg) 

  102. : base(msg) { 






  103. public class ListenerException : Exception { 

  104. public ListenerException(string msg) 

  105. : base(msg) { 





  106. #endregion 


  107. #region AddListener 

  108. //No parameters 

  109. static public void AddListener(string eventType, Callback handler) { 

  110. OnListenerAdding(eventType, handler); 

  111. eventTable[eventType] = (Callback)eventTable[eventType] + handler; 




  112. //Single parameter 

  113. static public void AddListener<T>(string eventType, Callback<T> handler) { 

  114. OnListenerAdding(eventType, handler); 

  115. eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler; 




  116. //Two parameters 

  117. static public void AddListener<T, U>(string eventType, Callback<T, U> handler) { 

  118. OnListenerAdding(eventType, handler); 

  119. eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler; 




  120. //Three parameters 

  121. static public void AddListener<T, U, V>(string eventType, Callback<T, U, V> handler) { 

  122. OnListenerAdding(eventType, handler); 

  123. eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] + handler; 



  124. #endregion 


  125. #region RemoveListener 

  126. //No parameters 

  127. static public void RemoveListener(string eventType, Callback handler) { 

  128. OnListenerRemoving(eventType, handler);  

  129. eventTable[eventType] = (Callback)eventTable[eventType] - handler; 

  130. OnListenerRemoved(eventType); 




  131. //Single parameter 

  132. static public void RemoveListener<T>(string eventType, Callback<T> handler) { 

  133. OnListenerRemoving(eventType, handler); 

  134. eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler; 

  135. OnListenerRemoved(eventType); 




  136. //Two parameters 

  137. static public void RemoveListener<T, U>(string eventType, Callback<T, U> handler) { 

  138. OnListenerRemoving(eventType, handler); 

  139. eventTable[eventType] = (Callback<T, U>)eventTable[eventType] - handler; 

  140. OnListenerRemoved(eventType); 




  141. //Three parameters 

  142. static public void RemoveListener<T, U, V>(string eventType, Callback<T, U, V> handler) { 

  143. OnListenerRemoving(eventType, handler); 

  144. eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] - handler; 

  145. OnListenerRemoved(eventType); 



  146. #endregion 


  147. #region Broadcast 

  148. //No parameters 

  149. static public void Broadcast(string eventType) { 

  150. #if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE 

  151. Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\""); 

  152. #endif 

  153. OnBroadcasting(eventType); 


  154. Delegate d; 

  155. if (eventTable.TryGetValue(eventType, out d)) { 

  156. Callback callback = d as Callback; 


  157. if (callback != null) { 

  158. callback(); 

  159. } else { 

  160. throw CreateBroadcastSignatureException(eventType); 








  161. //Single parameter 

  162. static public void Broadcast<T>(string eventType, T arg1) { 

  163. #if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE 

  164. Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\""); 

  165. #endif 

  166. OnBroadcasting(eventType); 


  167. Delegate d; 

  168. if (eventTable.TryGetValue(eventType, out d)) { 

  169. Callback<T> callback = d as Callback<T>; 


  170. if (callback != null) { 

  171. callback(arg1); 

  172. } else { 

  173. throw CreateBroadcastSignatureException(eventType); 








  174. //Two parameters 

  175. static public void Broadcast<T, U>(string eventType, T arg1, U arg2) { 

  176. #if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE 

  177. Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\""); 

  178. #endif 

  179. OnBroadcasting(eventType); 


  180. Delegate d; 

  181. if (eventTable.TryGetValue(eventType, out d)) { 

  182. Callback<T, U> callback = d as Callback<T, U>; 


  183. if (callback != null) { 

  184. callback(arg1, arg2); 

  185. } else { 

  186. throw CreateBroadcastSignatureException(eventType); 








  187. //Three parameters 

  188. static public void Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3) { 

  189. #if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE 

  190. Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\""); 

  191. #endif 

  192. OnBroadcasting(eventType); 


  193. Delegate d; 

  194. if (eventTable.TryGetValue(eventType, out d)) { 

  195. Callback<T, U, V> callback = d as Callback<T, U, V>; 


  196. if (callback != null) { 

  197. callback(arg1, arg2, arg3); 

  198. } else { 

  199. throw CreateBroadcastSignatureException(eventType); 







  200. #endregion 




  201. //This manager will ensure that the messenger's eventTable will be cleaned up upon loading of a new level. 

  202. public sealed class MessengerHelper : MonoBehaviour { 

  203. void Awake () 



  204. DontDestroyOnLoad(gameObject);  




  205. //Clean up eventTable every time a new level loads. 

  206. public void OnDisable() { 

  207. Messenger.Cleanup(); 





然后就可以开始使用了,Messager.Broadcast()这样就好比我们发送了一条广播。

  1. void Update() 



  2. if(Input.GetMouseButtonDown(0)) 



  3. Messenger.Broadcast("Send"); 





在需要这条广播的类中来接受它,同样是刚刚说的Script类。接受广播的标志是 Messager.AddListener()参数1表示广播的名称,参数2表示广播所调用的方法。

  1. using UnityEngine; 

  2. using System.Collections; 


  3. public class Script : MonoBehaviour { 


  4. void Awake() 



  5. Messenger.AddListener( "Send", DoSomething ); 



  6. public void DoSomething() 



  7. Debug.Log("DoSomething"); 





这样一来,只要发送名称为”Send”的方法,就可以在别的类中接收它了。

我们在说说如何通过广播来传递参数,这也是那天那个哥们主要问我的问题。(其实是维基百科上写的不是特别特别的清楚,那哥们误解了)在Callback中可以看出参数最多可以是三个,参数的类型是任意类型,也就是说我们不仅能传递 int float bool 还能传递gameObject类型。

如下所示,发送广播的时候传递了两个参数,参数1是一个游戏对象,参数2是一个int数值。

  1. void Update() 



  2. if(Input.GetMouseButtonDown(0)) 



  3. GameObject cube = GameObject.Find("Cube"); 

  4. Messenger.Broadcast<GameObject,int>("Send",cube,1980); 





然后是接受的地方 参数用<>存在一起。游戏对象也可以完美的传递。

  1. using UnityEngine; 

  2. using System.Collections; 


  3. public class Script : MonoBehaviour { 


  4. void Awake() 



  5. Messenger.AddListener<GameObject,int>( "Send", DoSomething ); 



  6. public void DoSomething(GameObject obj,int i) 



  7. Debug.Log("name " + obj.name + " id =" + i); 





如果传递一个参数

两个参数<T,T>

三个参数<T,T,T>

怎么样使用起来还是挺简单的吧?

我觉得项目中最好不要大量的使用代理事件这类的方法(根据需求而定),虽然可以让你的代码非常的简洁,但是它的效率不高大概比直接调用慢5-倍左右吧,就好比美好的东西一定都有瑕疵一样。 还记得Unity自身也提供了一种发送消息的方法吗?,用过的都知道效率也非常低下,虽然我们看不到它具体实现的源码是如何实现的,但是我觉得原理可能也是这样的。 欢迎和大家一起讨论与学习。

Unity消息简易框架 Advanced C# messenger的更多相关文章

  1. unity客户端基本框架(转载)

    框架概述: 基础系统的框架搭建,其中包括: UI框架(NGUI + MVC) 消息管理(Advanced CSharp Messenger) 网络层框架(Socket + Protobuf ) 表格数 ...

  2. 【转】Unity3D研究院之通过C#使用Advanced CSharp Messenger(五十)

    http://www.xuanyusong.com/archives/2165 Advanced CSharp Messenger 属于C#事件的一种. 维基百科中由详细的说明http://wiki. ...

  3. Unity3D之通过C#使用Advanced CSharp Messenger

    Advanced CSharp Messenger 属于C#事件的一种. 维基百科中由详细的说明http://wiki.unity3d.com/index.php?title=Advanced_CSh ...

  4. 【转】快速理解Kafka分布式消息队列框架

     from:http://blog.csdn.net/colorant/article/details/12081909 快速理解Kafka分布式消息队列框架 标签: kafkamessage que ...

  5. 快速理解Kafka分布式消息队列框架

    作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ ==是什么 == 简单的说,K ...

  6. Advanced CSharp Messenger

    http://wiki.unity3d.com/index.php?title=Advanced_CSharp_Messenger Author: Ilya Suzdalnitski Contents ...

  7. “一切都是消息”--MSF(消息服务框架)入门简介

    “一切都是消息”--这是MSF(消息服务框架)的设计哲学. MSF的名字是 Message Service Framework 的简称,中文名称:消息服务框架,它是PDF.NET框架的一部分. 1,M ...

  8. “一切都是消息”--MSF(消息服务框架)之【请求-响应】模式

    在前一篇, “一切都是消息”--MSF(消息服务框架)入门简介, 我们介绍了MSF基于异步通信,支持请求-响应通信模式和发布-订阅通信模式,并且介绍了如何获取MSF.今天,我们来看看如何使用MSF来做 ...

  9. “一切都是消息”--MSF(消息服务框架)之【发布-订阅】模式

    在上一篇,“一切都是消息”--MSF(消息服务框架)之[请求-响应]模式 ,我们演示了MSF实现简单的请求-响应模式的示例,今天来看看如何实现[发布-订阅]模式.简单来说,该模式的工作过程是: 客户端 ...

随机推荐

  1. Echarts 多曲线“断点”问题解决方法

    Echarts 用来做可视化曲线是非常优秀的一个库.建议使用 Echarts 作为项目的可视化图标库时,仔细研究 官方实例,根据需求来选择类似的示例,下载实例模板来开发,节省时间,减少出错,提高效率. ...

  2. hihoCoder1343 : Stable Members【BFS拓扑排序】

    题目链接:https://hihocoder.com/problemset/problem/1343 #1343 : Stable Members 时间限制:10000ms 单点时限:1000ms 内 ...

  3. MySQL半同步主从.md

    MySQL Semisynchronous Replication 复制架构衍生史 1.普通的replication,异步同步. 搭建简单,使用非常广泛,从mysql诞生之初,就产生了这种架构,性能非 ...

  4. 1051. [HAOI2006]受欢迎的牛【强连通分量】

    Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...

  5. BZOJ2694:Lcm——包看得懂/看不懂题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2694 Description 对于任意的>1的n gcd(a, b)不是n^2的倍数 也就是说 ...

  6. [USACO09MAR]Moon Mooing

    嘟嘟嘟 某谷的翻译挺迷的,简单来说就是给一个初值c,然后有两个函数f1 = a1 * x / d1 + b1, f2 = a2 * x / d2 + b2.把c分别带进去,所得的结果也递归带进去,这样 ...

  7. 【JavaScript】赛码网前端笔试本地环境搭建

    参考:https://hoofoo.me/article/2017-04-11/%E8%B5%9B%E7%A0%81%E7%BD%91%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AF ...

  8. 调试cnn-Sentence-Classifier遇到的问题

    运行train文件训练模型出现了以下错误: train文件在app文件目录下: raw_vectors.txt文件则在cnn-Sentence-Classifier目录下: 这是train代码调用re ...

  9. python110道面试题

    1.一行代码实现1--100之和 利用sum()函数求和 2.如何在一个函数内部修改全局变量 利用global 修改全局变量 3.列出5个python标准库 os:提供了不少与操作系统相关联的函数 s ...

  10. Python自动化之django orm之Q对象

    Python自动化之django orm之Q对象 什么是Q对象? Encapsulates filters as objects that can then be combined logically ...