高级C#信使(译) - Unity维基百科
高级C#信使
作者:Ilya Suzdalnitski
译自:http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger
描述
这是C#的一个高级版本的消息系统。当加载了一个新的场景(level)之后,它会自动清空事件表。这将防止程序员意外的调用已销毁的方法,从而有助于防止出现很多MissingReferenceExceptions。这个消息系统是基于Rod Hyde的CSharpMessenger和Magnus Wolffelt的CSharpMessenger Extended而研发的。
前言
我们的项目一旦引入了消息系统(CSharpMessenger Extended),我们就开始面对非常奇怪的BUGs。每次广播一个消息,U3D就会抛出MissingReferenceExceptions这个异常。提示会说消息处理器声明所在类已经被销毁。这个问题无处不在,并且没有一个合理的解释。不过,把消息处理器代码放到try-cache块中可以解决这个问题。我们知道,在代码中有大量的try-cache块不是一个好的解决方案。我们花费了一些时间,终于找到了问题的所在。
MissingReferenceException的原因和解决方案
是这样的,当加载一个新场景(level)(或者重新加载当前场景)时,MissingReferenceException的BUG就会出现。例如:我们有一个消息"start game",声明如下:
public class MainMenu : MonoBehaviour {
void Start ()
{
Messenger.AddListener("start game", StartGame);
}
void StartGame()
{
Debug.Log("StartGame called in" + gameObject); //This is the line that would throw an exception
}
void StartGameButtonPressed()
{
Messenger.Broadcast("start game");
}
}
乍一看,代码完全没有问题。但是在重新加载了这个场景之后,U3D将会抛出一个异常,提示说MainMenu对象已经被销毁。但是没有代码会销毁MainMenu脚本对象。
实际发生的是:
- 我们在信使中添加了一个"start game"消息监听器。
- StartGameButtonPressed方法被调用,广播了"start game"消息。
- 我们使用Application.LoadLevel重新加载了这个场景(level)。
- 重复第1步。
- 重复第2步。
在相应的步骤,信使的事件表里是这个样子的:
- 在第1步:{ "start game", mainMenu1- > StartGame(); }
- 在第4步:{ "start game", mainMenu1- > StartGame(); } { "start game", mainMenu2- > StartGame(); }
所以在第4步我们有两个"start game"消息处理器——第1个是已经销毁的MainMenu对象(在重新加载场景时被销毁),第2个是当前有效的MainMenu对象。结果是这样的,当我们在重新加载场景后广播"start game"消息时,信使把两个消息处理器(已经销毁的和当前有效的)都调用了。这里就是MissingReferenceException出现的源头。 那么解决方法显而易见——在卸载场景之后清空eventTable。在程序员方面不用做任何事来清空这个表,这一过程将自动完成。
信使
我们很高兴向你展示一个高级版本的C#消息系统。
用法
事件监听器
void OnPropCollected( PropType propType ) {
if (propType == PropType.Life)
livesAmount++;
}
注册事件监听器
void Start() {
Messenger.AddListener< Prop >( "prop collected", OnPropCollected );
}
注销事件监听器
Messenger.RemoveListener< Prop > ( "prop collected", OnPropCollected );
广播事件
public void OnTriggerEnter(Collider _collider)
{
Messenger.Broadcast< PropType > ( "prop collected", _collider.gameObject.GetComponent<Prop>().propType );
}
清空信使
当加载一个新的场景(level)时,信使会自动清空它的eventTable。这将确保信使的eventTable被清空,并且将使我们免于意外的MissingReferenceExceptions。如果你想手动清空管理器的eventTable,你可以调用Messenger.Cleanup()。
永久信使
如果你想要某个消息幸免于Cleanup,使用Messenger.MarkAsPermanent(string)来标记它既可。它可能用于这样的场合:某个类响应不同场景(level)的消息广播。
杂项
打印所有消息
为了调试的目的,你可以把信使的shouldLogAllMessages设置成true。这样,在调用信使的任何方法时都会打印消息。
从其他信使过渡
为了快速把旧的消息系统CSharpMessenger转变成高级的消息系统,请执行下面的步骤:
- 在MonoDevelop中打开“在文件中查找/替换”对话框。
- 在查找域输入:Messenger<([^<>]+)>.([A-Za-z0-9_]+)
- 在替换域输入:Messenger.$2<$1>
- 选择域:所有解决方案
- 勾选“正则搜索”复选框
- 按下“替换”按钮
代码
想要信使顺利运作需要两个文件:Callback.cs和Messenger.cs。
Callback.cs
public delegate void Callback();
public delegate void Callback<T>(T arg1);
public delegate void Callback<T, U>(T arg1, U arg2);
public delegate void Callback<T, U, V>(T arg1, U arg2, V arg3);
Messenger.cs
/*
* Advanced C# messenger by Ilya Suzdalnitski. V1.0
*
* Based on Rod Hyde's "CSharpMessenger" and Magnus Wolffelt's "CSharpMessenger Extended".
*
* Features:
* Prevents a MissingReferenceException because of a reference to a destroyed message handler.
* Option to log all messages
* Extensive error detection, preventing silent bugs
*
* Usage examples:
1. Messenger.AddListener<GameObject>("prop collected", PropCollected);
Messenger.Broadcast<GameObject>("prop collected", prop);
2. Messenger.AddListener<float>("speed changed", SpeedChanged);
Messenger.Broadcast<float>("speed changed", 0.5f);
*
* Messenger cleans up its evenTable automatically upon loading of a new level.
*
* Don't forget that the messages that should survive the cleanup, should be marked with Messenger.MarkAsPermanent(string)
*
*/ //#define LOG_ALL_MESSAGES
//#define LOG_ADD_LISTENER
//#define LOG_BROADCAST_MESSAGE
#define REQUIRE_LISTENER using System;
using System.Collections.Generic;
using UnityEngine; static internal class Messenger {
#region Internal variables //Disable the unused variable warning
#pragma warning disable 0414
//Ensures that the MessengerHelper will be created automatically upon start of the game.
static private MessengerHelper messengerHelper = ( new GameObject("MessengerHelper") ).AddComponent< MessengerHelper >();
#pragma warning restore 0414 static public Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>(); //Message handlers that should never be removed, regardless of calling Cleanup
static public List< string > permanentMessages = new List< string > ();
#endregion
#region Helper methods
//Marks a certain message as permanent.
static public void MarkAsPermanent(string eventType) {
#if LOG_ALL_MESSAGES
Debug.Log("Messenger MarkAsPermanent \t\"" + eventType + "\"");
#endif permanentMessages.Add( eventType );
} static public void Cleanup()
{
#if LOG_ALL_MESSAGES
Debug.Log("MESSENGER Cleanup. Make sure that none of necessary listeners are removed.");
#endif List< string > messagesToRemove = new List<string>(); foreach (KeyValuePair<string, Delegate> pair in eventTable) {
bool wasFound = false; foreach (string message in permanentMessages) {
if (pair.Key == message) {
wasFound = true;
break;
}
} if (!wasFound)
messagesToRemove.Add( pair.Key );
} foreach (string message in messagesToRemove) {
eventTable.Remove( message );
}
} static public void PrintEventTable()
{
Debug.Log("\t\t\t=== MESSENGER PrintEventTable ==="); foreach (KeyValuePair<string, Delegate> pair in eventTable) {
Debug.Log("\t\t\t" + pair.Key + "\t\t" + pair.Value);
} Debug.Log("\n");
}
#endregion #region Message logging and exception throwing
static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded) {
#if LOG_ALL_MESSAGES || LOG_ADD_LISTENER
Debug.Log("MESSENGER OnListenerAdding \t\"" + eventType + "\"\t{" + listenerBeingAdded.Target + " -> " + listenerBeingAdded.Method + "}");
#endif if (!eventTable.ContainsKey(eventType)) {
eventTable.Add(eventType, null );
} Delegate d = eventTable[eventType];
if (d != null && d.GetType() != listenerBeingAdded.GetType()) {
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));
}
} static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved) {
#if LOG_ALL_MESSAGES
Debug.Log("MESSENGER OnListenerRemoving \t\"" + eventType + "\"\t{" + listenerBeingRemoved.Target + " -> " + listenerBeingRemoved.Method + "}");
#endif if (eventTable.ContainsKey(eventType)) {
Delegate d = eventTable[eventType]; if (d == null) {
throw new ListenerException(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType));
} else if (d.GetType() != listenerBeingRemoved.GetType()) {
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));
}
} else {
throw new ListenerException(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType));
}
} static public void OnListenerRemoved(string eventType) {
if (eventTable[eventType] == null) {
eventTable.Remove(eventType);
}
} static public void OnBroadcasting(string eventType) {
#if REQUIRE_LISTENER
if (!eventTable.ContainsKey(eventType)) {
throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but no listener found. Try marking the message with Messenger.MarkAsPermanent.", eventType));
}
#endif
} static public BroadcastException CreateBroadcastSignatureException(string eventType) {
return new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
} public class BroadcastException : Exception {
public BroadcastException(string msg)
: base(msg) {
}
} public class ListenerException : Exception {
public ListenerException(string msg)
: base(msg) {
}
}
#endregion #region AddListener
//No parameters
static public void AddListener(string eventType, Callback handler) {
OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] + handler;
} //Single parameter
static public void AddListener<T>(string eventType, Callback<T> handler) {
OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
} //Two parameters
static public void AddListener<T, U>(string eventType, Callback<T, U> handler) {
OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
} //Three parameters
static public void AddListener<T, U, V>(string eventType, Callback<T, U, V> handler) {
OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] + handler;
}
#endregion #region RemoveListener
//No parameters
static public void RemoveListener(string eventType, Callback handler) {
OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] - handler;
OnListenerRemoved(eventType);
} //Single parameter
static public void RemoveListener<T>(string eventType, Callback<T> handler) {
OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;
OnListenerRemoved(eventType);
} //Two parameters
static public void RemoveListener<T, U>(string eventType, Callback<T, U> handler) {
OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] - handler;
OnListenerRemoved(eventType);
} //Three parameters
static public void RemoveListener<T, U, V>(string eventType, Callback<T, U, V> handler) {
OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] - handler;
OnListenerRemoved(eventType);
}
#endregion #region Broadcast
//No parameters
static public void Broadcast(string eventType) {
#if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE
Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\"");
#endif
OnBroadcasting(eventType); Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback callback = d as Callback; if (callback != null) {
callback();
} else {
throw CreateBroadcastSignatureException(eventType);
}
}
} //Single parameter
static public void Broadcast<T>(string eventType, T arg1) {
#if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE
Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\"");
#endif
OnBroadcasting(eventType); Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T> callback = d as Callback<T>; if (callback != null) {
callback(arg1);
} else {
throw CreateBroadcastSignatureException(eventType);
}
}
} //Two parameters
static public void Broadcast<T, U>(string eventType, T arg1, U arg2) {
#if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE
Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\"");
#endif
OnBroadcasting(eventType); Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T, U> callback = d as Callback<T, U>; if (callback != null) {
callback(arg1, arg2);
} else {
throw CreateBroadcastSignatureException(eventType);
}
}
} //Three parameters
static public void Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3) {
#if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE
Debug.Log("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\"");
#endif
OnBroadcasting(eventType); Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T, U, V> callback = d as Callback<T, U, V>; if (callback != null) {
callback(arg1, arg2, arg3);
} else {
throw CreateBroadcastSignatureException(eventType);
}
}
}
#endregion
} //This manager will ensure that the messenger's eventTable will be cleaned up upon loading of a new level.
public sealed class MessengerHelper : MonoBehaviour {
void Awake ()
{
DontDestroyOnLoad(gameObject);
} //Clean up eventTable every time a new level loads.
public void OnLevelWasLoaded(int unused) {
Messenger.Cleanup();
}
}
高级C#信使(译) - Unity维基百科的更多相关文章
- 通过维基API实现维基百科查询功能
通过英文维基的免费API,可以实现对维基百科的搜索查询或者标题全文查询等,尝试了一下通过title实现全文查询,返回的结果是wikitext格式,暂时不知道该如何应用,所以仅实现了查询功能,可以返回最 ...
- 中英文维基百科语料上的Word2Vec实验
最近试了一下Word2Vec, GloVe 以及对应的python版本 gensim word2vec 和 python-glove,就有心在一个更大规模的语料上测试一下,自然而然维基百科的语料进入了 ...
- 开源共享一个训练好的中文词向量(语料是维基百科的内容,大概1G多一点)
使用gensim的word2vec训练了一个词向量. 语料是1G多的维基百科,感觉词向量的质量还不错,共享出来,希望对大家有用. 下载地址是: http://pan.baidu.com/s/1boPm ...
- ESMOD北京高级时装艺术学校_百度百科
ESMOD北京高级时装艺术学校_百度百科 ESMOD北京高级时装艺术学校
- 学习笔记TF018:词向量、维基百科语料库训练词向量模型
词向量嵌入需要高效率处理大规模文本语料库.word2vec.简单方式,词送入独热编码(one-hot encoding)学习系统,长度为词汇表长度的向量,词语对应位置元素为1,其余元素为0.向量维数很 ...
- wikipedia 维基百科 语料 获取 与 提取 处理 by python3.5
英文维基百科 https://dumps.wikimedia.org/enwiki/ 中文维基百科 https://dumps.wikimedia.org/zhwiki/ 全部语言的列表 https: ...
- 使用JWPL (Java Wikipedia Library)操作维基百科数据
使用JWPL (Java Wikipedia Library)操作维基百科数据 1. JWPL介绍 JWPL(Java Wikipedia Library)是一个开源的访问wikipeida数据的Ja ...
- Kaggle比赛冠军经验分享:如何用 RNN 预测维基百科网络流量
Kaggle比赛冠军经验分享:如何用 RNN 预测维基百科网络流量 from:https://www.leiphone.com/news/201712/zbX22Ye5wD6CiwCJ.html 导语 ...
- Windows下基于python3使用word2vec训练中文维基百科语料(二)
在上一篇对中文维基百科语料处理将其转换成.txt的文本文档的基础上,我们要将为文本转换成向量,首先都要对文本进行预处理 步骤四:由于得到的中文维基百科中有许多繁体字,所以我们现在就是将繁体字转换成简体 ...
随机推荐
- There is no getter for property named 'notice' in 'class com.game.domain.Notices'
在插入数据时报错:There is no getter for property named 'notice' in 'class com.game.domain.Notices' 四月 11, 20 ...
- java 面试随笔
---恢复内容开始--- 1.自我介绍 2.你在项目开发过程中遇到的那些问题! 3.懂bootstrap么?简单介绍一下 4.spring的会话数据是怎样的. 5.为什么会有session 因为htt ...
- 5W2H方法
5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏 ...
- vs2013 查找进行的过程中被停止
VS"Find in Files"失效的解决方法一:让VS窗口获得焦点,依次按以下快捷键Ctrl+BreakCtrl+Scroll LockAlt+Break VS"Fi ...
- LINQ 学习之筛选条件
从list 里遍历出 id="DM" 且 code="0000" 的数据 var tes1= from u in list.where u.ID == & ...
- 【刷题】BZOJ 3262 陌上花开
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...
- FFT常数优化(共轭优化)
最近闲着无聊研究了下\(FFT\)的常数优化,大概就是各种\(3\)次变\(2or1.5\)次之类的,不过没见过啥题卡这个的吧. 关于\(FFT\)可以看这里:浅谈FFT&NTT. 关于复数 ...
- Android指纹识别API讲解,让你有更好的用户体验
我发现了一个比较怪的现象.在iPhone上使用十分普遍的指纹认证功能,在Android手机上却鲜有APP使用,我简单观察了一下,发现Android手机上基本上只有支付宝.微信和极少APP支持指纹认证功 ...
- JUnit中按照顺序执行测试方式
很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前面,然后是修改或者查询,最后才是删除,以 ...
- [UVALive 3902] Network
图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...