public static void UnsubscribeFromEvent(EventDataCallback callback, Component component, EventType type) { CallbackComponentPair pair = new CallbackComponentPair(callback, component); if (componentEventsData.ContainsKey(type)) { componentEventsData[type].Unsubscribe(pair); } }
/// <summary> /// Subscribe to an event with a callback. This removes all previous subscriptions for this event. /// </summary> /// <param name="callback"></param> /// <param name="type"></param> public static void OverrideSubscription(EventDataCallback callback, Component component, EventType type) { CallbackComponentPair pair = new CallbackComponentPair(callback, component); ComponentEventsContainer cont = new ComponentEventsContainer(); cont.AddPair(pair); componentEventsData.Add(type, cont); }
public void Unsubscribe(CallbackComponentPair pair) { for (int i = 0; i < pairs.Count; i++) { if (pair.Equals(pairs[i])) { pairs.RemoveAt(i); i--; return; } } }
public override bool Equals(object obj) { if (GetType() != obj.GetType()) { return(false); } CallbackComponentPair other = (CallbackComponentPair)obj; bool type = CallbackWithData == other.CallbackWithData; bool comp = Component.Equals(other.Component); return(type && comp); }
/// <summary> /// Subscribe to an event with a callback from a component. The subscription is removed automatically when the component is destroyed. /// </summary> /// <param name="callback"></param> /// <param name="component"></param> /// <param name="type"></param> public static void SubscribeToEvent(EventDataCallback callback, Component component, EventType type) { CallbackComponentPair pair = new CallbackComponentPair(callback, component); if (!componentEventsData.ContainsKey(type)) { ComponentEventsContainer cont = new ComponentEventsContainer(); cont.AddPair(pair); componentEventsData.Add(type, cont); } else { componentEventsData[type].AddPair(pair); } }
/// <summary> /// Calls callbacks if the component which subscribed to it isn't null. Automatically removes subscriptions for destroyed components/gameobjects. /// </summary> public void FireIfNotNull(CallbackData data) { for (int i = 0; i < pairs.Count; i++) { CallbackComponentPair pair = pairs[i]; if (pair.Component == null) { pairs.RemoveAt(i); i--; continue; } pair.CallbackWithData(data); } }
public void AddPair(CallbackComponentPair pair) { pairs.Add(pair); }