static public class Messenger<T, U>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;
static public void AddListener(string eventType, Callback<T, U> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
}
static public void RemoveListener(string eventType, Callback<T, U> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}
static public void Broadcast(string eventType, T arg1, U arg2)
{
Broadcast(eventType, arg1, arg2, MessengerInternal.DEFAULT_MODE);
}
static public void Broadcast(string eventType, T arg1, U arg2, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
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 MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
消息类,里面保存所有的事件(对应一个方法),一个事件对应一个string的事件类型
然后可以通过broadcast触发事件(调用对应 的方法)
相当于消息类,注册监听消息的方法,每个消息有一个类型,然后广播消息,根据消息类型调用方法