//------------------------------------------------------------------------/ // Methods //------------------------------------------------------------------------/ /// <summary> /// Dispatches the event onto the given target /// </summary> /// <returns></returns> public bool Dispatch() { if (!hasType) { return(false); } if (eventInstance == null) { eventInstance = StratusEvent.Instantiate(type, eventData); } switch (scope) { case StratusEvent.Scope.GameObject: foreach (var target in targets) { if (target) { target.Dispatch(eventInstance, type.Type); } } break; case StratusEvent.Scope.Scene: Scene.Dispatch(eventInstance, type.Type); break; } return(true); }
public static StratusEvent Instantiate(Type type, string data) { StratusEvent instance = Instantiate(type); JsonUtility.FromJsonOverwrite(data, instance); return(instance); }
/// <summary> /// Dispatches the event on the next frame. /// </summary> /// <typeparam name="T">The event class.</typeparam> /// <param name="obj">The object to which to dispatch to.</param> /// <param name="eventObj">The event object we are sending.</param> /// <returns></returns> public static IEnumerator DispatchNextFrame(GameObject obj, StratusEvent eventObj, Type type) { // Wait 1 frame yield return(0); // Dispatch the event Dispatch(obj, eventObj, type); }
/// <summary> /// Dispatches the given event of the specified type onto the object. /// </summary> /// <typeparam name="T">The event class.</typeparam> /// <param name="obj">The object to which to connect to.</param> /// <param name="eventObj">The name of the event to which to listen for.</param> /// <param name="nextFrame">Whether to send this event on the next frame.</param> public static void Dispatch(GameObject obj, StratusEvent eventObj, System.Type type, bool nextFrame = false) { var key = type.ToString(); #if STRATUS_EVENTS_DEBUG if (logging.Connect) { Trace.Script("'" + key + "' to '" + obj.name + "'"); } #endif // If this a delayed dispatch... if (nextFrame) { instance.StartCoroutine(DispatchNextFrame(obj, eventObj, type)); } // Check if the object has been registered onto the event system. // If not, it will be. CheckRegistration(obj); // If there is no delegate registered to this object, do nothing. if (!HasDelegate(obj, key)) { #if STRATUS_EVENTS_DEBUG if (logging.Dispatch) { Trace.Script("No delegate registered to " + obj.name + " for " + eventObj.ToString()); } #endif return; } // If we are watching events of this type bool watching = false; if (instance.eventWatchList.Contains(key)) { watching = true; } // Invoke the method for every delegate var delegateMap = StratusEvents.instance.dispatchMap[obj][key]; DelegateTypeList delegatesToRemove = null; foreach (var deleg in delegateMap) { // If we are watching events of this type if (watching) { StratusDebug.Log("Invoking member function on " + deleg.Target.ToString()); } // Do a lazy delete if it has been nulled out? if (IsNull(deleg.Method) || IsNull(deleg.Target)) { if (delegatesToRemove == null) { delegatesToRemove = new DelegateTypeList(); } delegatesToRemove.Add(deleg); continue; } deleg.DynamicInvoke(eventObj); } // If any delegates were found to be null, remove them (lazy delete) if (delegatesToRemove != null) { foreach (var deleg in delegatesToRemove) { delegateMap.Remove(deleg); } } }