/// <summary> /// attempts to trigger the pre-registered method or delegate associated /// with 'eventName' and the target object. if 'options' is set to /// 'TargetEventOptions.RequireReceiver' there will be an error if /// no such callback can be found. /// </summary> public static void Send(object target, string eventName, TargetEventOptions options = TargetEventOptions.DontRequireReceiver) { retry : Delegate callback = TargetEventHandler.GetCallback(target, eventName, false, 0, options); if (callback == null) { TargetEventHandler.OnNoReceiver(eventName, options); return; } // NOTE: if the below 'try' fails, it is likely because we have an event // with the same name and amount of arguments + return value as another // event, in which case the dictionary happily returned an event with an // incompatible parameter signature = BOOM. we catch this and try again // with an underscore appended to the event name. if 'Register' has added // a matching event with underscores in the name we'll find it sooner or // later. if not, 'callback' will end up null and we'll return gracefully // on the above null check (which is also what will happen if the invocation // crashes for any other reason). try { ((Action)callback).Invoke(); } catch { eventName += "_"; goto retry; } }
/// <summary> /// This method will send an event to an object of type 'UnityEngine.Component', /// then onward to its transform and all ancestor transforms recursively until /// a callback registered under 'eventName' is found in any of the objects. NOTES: /// 1) only the first matching callback detected will execute. The event will fail /// if the scan reaches the scene root with no match found. 2) it is best to register /// _transforms_ and not other types of component for use with 'SendUpwards', since /// components will be ignored unless the event is sent specifically to the correct /// component. /// </summary> public static void SendUpwards(Component target, string eventName, TargetEventOptions options = TargetEventOptions.DontRequireReceiver) { retry : Delegate callback = TargetEventHandler.GetCallback(target, eventName, true, 0, options); if (callback == null) { TargetEventHandler.OnNoReceiver(eventName, options); return; } try { ((Action)callback).Invoke(); } catch { eventName += "_"; goto retry; } }
public static void UnRegister(Component component) { TargetEventHandler.UnRegister(component); }
public static void UnRegister(object target) { TargetEventHandler.UnRegister(target, null, null); }
/// <summary> /// removes a callback from the target event handler by target object and event /// name. if name is null, the whole object will be unregistered. NOTES: 1) this /// method will disable _all_ events with the specified name and target object, /// regardless of the generic signature used. 2) be careful when omitting /// 'eventName': if doing so in a component 'OnDisable' method, all events for /// all other components on the target will be unregistered. /// </summary> public static void UnRegister(object target, string eventName, Action callback) { TargetEventHandler.UnRegister(target, eventName, callback); }
/// <summary> /// registers a callback (method or delegate) with the target object. /// the callback can be triggered later by sending the event 'eventName'. /// </summary> public static void Register(object target, string eventName, Action callback) { TargetEventHandler.Register(target, eventName, (Delegate)callback, 0); }