Пример #1
0
        /// <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; }
        }
Пример #2
0
 /// <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; }
 }