static void OnListenerAdding(MessengerEventType eventkey, Delegate listenerDelegate) { if (!eventTable.ContainsKey(eventkey)) { eventTable.Add(eventkey, null); } }
static public void OnListenerRemoved(MessengerEventType eventType) { if (eventTable[eventType] == null) { eventTable.Remove(eventType); } }
static void OnBroadcast(MessengerEventType eventtype) { if (!eventTable.ContainsKey(eventtype)) { throw new ListenerException("不包含此监听"); } }
static public void MarkAsPermanent(MessengerEventType eventType) { #if LOG_ALL_MESSAGES Debug.Log("Messenger MarkAsPermanent \t\"" + eventType + "\""); #endif permanentMessages.Add(eventType); }
static public void OnBroadcasting(MessengerEventType 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 }
public static void Broadcast(MessengerEventType eventtype) { OnBroadcast(eventtype); CallBack callback; if (eventTable.ContainsKey(eventtype)) { callback = eventTable[eventtype] as CallBack; callback();//如果不为空调用,unity2017以下不可简写 } }
public static void Broadcast <T, D, U>(MessengerEventType eventtype, T t, D d, U u) { CallBack <T, D, U> callback; if (eventTable.ContainsKey(eventtype)) { callback = eventTable[eventtype] as CallBack <T, D, U>; if (null != callback) { callback(t, d, u); //如果不为空调用,unity2017以下不可简写 } } }
public static void Broadcast <T>(MessengerEventType eventtype, T t) { OnBroadcast(eventtype); CallBack <T> callback; if (eventTable.ContainsKey(eventtype)) { callback = eventTable[eventtype] as CallBack <T>; if (null != callback) { callback(t);//如果不为空调用,unity2017以下不可简写 } } }
static public void OnListenerAdding(MessengerEventType 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(MessengerEventType eventType, Delegate listenerBeingRemoved) { 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)); } }
//Three parameters static public void Broadcast <T, U, V>(MessengerEventType 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)) { MessengerCallback <T, U, V> MessengerCallback = d as MessengerCallback <T, U, V>; if (MessengerCallback != null) { MessengerCallback(arg1, arg2, arg3); } else { throw CreateBroadcastSignatureException(eventType); } } }
static public void OnListenerRemoving(MessengerEventType 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 RemoveListener <T, D>(MessengerEventType eventType, CallBack <T, D> handler) { OnListenerRemoving(eventType, handler); eventTable[eventType] = (CallBack <T, D>)eventTable[eventType] - handler; OnListenerRemoved(eventType); }
/// <summary> /// Removes the listener. /// </summary> /// <param name="eventType">Event type.</param> /// <param name="handler">Handler.</param> static public void RemoveListener(MessengerEventType eventType, Callback handler) { RemoveListener(eventType.ToString(), handler); }
static public BroadcastException CreateBroadcastSignatureException(MessengerEventType eventType) { return(new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType))); }
/// <summary> /// Broadcast the specified eventType and mode. /// </summary> /// <param name="eventType">Event type.</param> /// <param name="mode">Mode.</param> static public void Broadcast (MessengerEventType eventType, MessengerMode mode) { Broadcast (eventType.ToString (), mode); }
public static void AddListener <T, D, U>(MessengerEventType eventtype, CallBack <T, D, U> handler) { OnListenerAdding(eventtype, handler); eventTable[eventtype] = (eventTable[eventtype] as CallBack <T, D, U>) + (handler); }
//Three parameters static public void AddListener <T, U, V>(MessengerEventType eventType, MessengerCallback <T, U, V> handler) { OnListenerAdding(eventType, handler); eventTable[eventType] = (MessengerCallback <T, U, V>)eventTable[eventType] + handler; }
//Three parameters static public void RemoveListener <T, U, V>(MessengerEventType eventType, MessengerCallback <T, U, V> handler) { OnListenerRemoving(eventType, handler); eventTable[eventType] = (MessengerCallback <T, U, V>)eventTable[eventType] - handler; OnListenerRemoved(eventType); }
/// <summary> /// Broadcast the specified eventType. /// </summary> /// <param name="eventType">Event type.</param> static public void Broadcast(MessengerEventType eventType) { Broadcast(eventType.ToString(), MessengerInternal.DEFAULT_MODE); }
/// <summary> /// Broadcast the specified eventType. /// </summary> /// <param name="eventType">Event type.</param> static public void Broadcast (MessengerEventType eventType) { Broadcast (eventType.ToString (), MessengerInternal.DEFAULT_MODE); }
/// <summary> /// Broadcast the specified eventType and mode. /// </summary> /// <param name="eventType">Event type.</param> /// <param name="mode">Mode.</param> static public void Broadcast(MessengerEventType eventType, MessengerMode mode) { Broadcast(eventType.ToString(), mode); }
/// <summary> /// Removes the listener. /// </summary> /// <param name="eventType">Event type.</param> /// <param name="handler">Handler.</param> static public void RemoveListener (MessengerEventType eventType, Callback handler) { RemoveListener (eventType.ToString (), handler); }