该博客,只为解析,解析,解析,已经整理好,已经整理好,已经整理好。代码核心原理套用网上最流行的那一套,也是最经常使用游戏开发适用的消息机制。这里面加上本身的一些优化,极大的修正(哈哈),实测,没问题。万一要是出现问题,欢迎童鞋能够留言给我修正。html
有童鞋可能会好奇,unity里面不是有本身的一套消息发送, 例如什么SendMessage,这...这个几乎是不能用的。设计模式
为啥不能用,看看如下是网上给的解释,本身玩玩demo仍是能够用,可是实际开发,是几乎不能用的。函数
I:它实现的是一种伪监听者模式,利用的是反射机制。优化
II:SendMessage效率不高,由于每次调用的时候都会去遍历检测自身或者子节点上要调用的方法。ui
III:须要知道响应事件的物件,还须要一个响应函数的函数名字符串做为参数,若是咱们有多个物件都要响应某个事件怎么办呢,或者咱们不知道有哪些物件要响应事件怎么办呢。(前面两句话比较抽象,这句话总能看的懂吧)this
(若有不理解委托,事件能够参考个人这篇帖子:https://www.cnblogs.com/u3ddjw/p/9920994.html)spa
通俗易懂点讲,就是 一个物体发出消息,另一个,或者几个物体能够同时接收到这一消息并做出各自不一样的行为(反馈,处理)。设计
那么,首先,咱们想到,须要什么?3d
I: 咱们须要的是消息(实例),发送者。 消息(实例)+发送者=咱们须要的消息,就可以处理任何消息。code
II:怎么把这个消息发送出去(消息处理中心)。
III:发送者发送(分发)消息的行为
IV:接收消息。
换一种说法:发布-订阅模式。举例就是定报纸,你跟邮局定了报纸,邮局就会在指定时间把报纸发下来给你;中间若是你不须要报纸了,那么你就取消这个订阅,邮局就不会发给你了。
图解:
public class Notification { /// <summary> /// 发送者 /// </summary> public GameObject sender; /// <summary> /// 消息内容 /// </summary> public EventArgs param; /// <summary> /// 构造函数 (初始化) /// </summary> ///<param name="sender">通知发送者 ///<param name="param">通知内容 public Notification(GameObject sender, EventArgs param) { this.sender = sender; this.param = param; }
public Notification() { } /// <summary> /// 构造函数 /// </summary> ///<param name="param"> public Notification(EventArgs param) { this.sender = null; this.param = param; } } /// <summary> /// 传递的消息,这个是消息类中的具体消息种类 类 /// </summary> public class EventArgsTest : EventArgs { public int id; public string name; }
Notification是一个稍微抽象一点的消息类,要传递一个消息(类),我前面说到了,确定是须要知道具体发送者和具体消息类的。
而具体消息类,就是后面的EventArgsTest,这个是继承于System.EventArgs,该类是自定义类,看到后面,可能会理解为何这样继承。
public delegate void NotificationDelegate(Notification notific);
声明一个委托传递上面所说的消息类的委托,这边通俗一点来说就是:声明一个能够传递Notification 参数的方法。至于委托的用法这里就不详诉了。
public class NotificationCenter { private static NotificationCenter instance = null; public static NotificationCenter Get() { if (instance == null) { instance = new NotificationCenter(); return instance; } return instance; } private Dictionary<uint, NotificationDelegate> eventListeners = new Dictionary<uint, NotificationDelegate>(); public void AddEventListener(uint eventKey, NotificationDelegate listener) { if (!HasEventListener(eventKey)) { NotificationDelegate del = null; //定义方法 eventListeners[eventKey] = del;// 给委托变量赋值 } eventListeners[eventKey] += listener; //注册接收者的监听 } public void RemoveEventListener(uint eventKey,NotificationDelegate listener) { if (!HasEventListener(eventKey)) return; eventListeners[eventKey] -= listener; if (eventListeners[eventKey] == null) { RemoveEventListener(eventKey); } } public void RemoveEventListener(uint eventKey) { eventListeners.Remove(eventKey); } /// <summary> /// 分发事件,不须要知道发送者的状况 /// </summary> /// <param name="eventKey"></param> /// <param name="notific"></param> public void PostDispatchEvent(uint eventKey, Notification notific) { if (!HasEventListener(eventKey)) return; // eventListeners[eventKey].Invoke(notific); eventListeners[eventKey](notific); } /// <summary> /// 分发事件,须要知道发送者,具体消息的状况 /// </summary> ///<param name="eventKey">事件Key ///<param name="sender">发送者 ///<param name="param">通知内容 public void PostDispatchEvent(uint eventKey, GameObject sender, EventArgs param) { if (!HasEventListener(eventKey)) return; eventListeners[eventKey](new Notification(sender, param)); } public void PostDispatchEvent(uint eventKey) { if (!HasEventListener(eventKey)) return; eventListeners[eventKey](new Notification()); } /// <summary> /// 分发事件,不须要知道任何,只须要知道发送过来消息了 /// </summary> ///<param name="eventKey">事件Key ///<param name="param">通知内容 public void PostDispatchEvent(uint eventKey, EventArgs param) { if (!HasEventListener(eventKey)) return; eventListeners[eventKey](new Notification(param)); } /// <summary> /// 是否存在指定事件的监听器 /// </summary> public bool HasEventListener(uint eventKey) { return eventListeners.ContainsKey(eventKey); } }
该消息机制的核心,难点也就是在这里了。
首先,既然是消息处理中心,确定是须要一个存放传递消息(上面那个声明的委托)的容器,因而声明一个
private Dictionary<uint, OnNotification> eventListeners = new Dictionary<uint, OnNotification>();
增长,移除 传递消息(上面那个声明的委托),不就是如下代码,须要注意的是
eventListeners[eventKey] -= listener;//取消接收者的监听 eventListeners.Remove(eventKey);//移除存放在在eventListeners为eventKey的传递消息(上面那个委托)
if (!HasEventListener(eventKey)) { eventListeners[eventKey] = listener; //注册接收者的监听 } else { eventListeners[eventKey] += listener; //注册接收者的监听,这个用法,是委托的一种机制,不理解的本身去百度看看委托咋回事。 }
这样,如何存储消息作完了。
/// <summary> /// 消息类型,枚举列出,调用时须要强转为uint /// </summary> public enum ENotificationMsgType // 消息发送的枚举值,应该转为uint型 { ENull = 0, //Test
ELoadResProgress = 1, }
以上代码,写枚举,纯是为了提升代码可读性及可维护性,C#中多写枚举,少写那种莫名其妙的 int变量,真心感谢第一家公司对个人影响,保持良好的代码可读性。
EventArgsTest args = new EventArgsTest(); args.id = 3; args.name = "我是Test发送的 name 消息哦"; NotificationCenter.Get().PostDispatchEvent((uint)ENotificationMsgType.ENull, args); // NotificationCenter.Get().PostDispatchEvent((uint)ENotificationMsgType.ENull); //我就是通知,不发送具体啥消息,也是能够的哦
这边须要理解的是 PostDispatchEvent,这个方法,这边我 写了三重重载,由于发送消息分三种状况,如注释那样
{
只须要通知发送,不须要知道发送的具体消息类型,也不须要发送者。
只须要发送具体消息类型,不须要发送者。
须要发送具体消息类型,须要发送者。
}
void Awake() { NotificationCenter.Get().AddEventListener((uint)ENotificationMsgType.ENull, UpdateTest); } void OnDestroy() { NotificationCenter.Get().RemoveEventListener((uint)ENotificationMsgType.ENull, UpdateTest); } void UpdateTest(Notification e) { EventArgsTest args = e.param as EventArgsTest; if (args != null) { string strName = args.name; int strId = args.id; } }
可能你会奇怪,注册事件和移除事件为何这样写。这是一种标准写法。
写初始(Start),结束(OnDestroy),使得每一个消息拥有一个本身的生命周期。
(2018年12月15日,补充更新,推荐使用这个版本,可是也上述比较本质都是基本同样的)
该版原本自siki学院:给出视频连接 http://www.sikiedu.com/my/course/304
该版本优点:①极大减小代码量。
②极大节省人力。
上述版本传递消息,自定义 EventArgs的子类,遇到特殊类型,声明的类的类型不少,增大了代码量,这是极大的弊端。
该版本也是我无心间看到的,感受很不错,特来补充。
最终,如如有讲述不清,错误之处,欢迎指正。