/// <summary> /// Unsubscribe a listener from an event in the specific update tick. /// </summary> /// <param name="target">The target to unsubscribe from.</param> /// <param name="eventCallback">The event callback</param> /// <param name="tick">The update type to unsubscribe to.</param> /// <typeparam name="T_Event">The event</typeparam> public static void Unsubscribe <T_Event>(EventTarget target, Action <T_Event> eventCallback, EventUpdateTick tick) where T_Event : struct { if (tick == EventUpdateTick.FixedUpdate) { _fixedUpdateSystem.Unsubscribe(target, eventCallback); } else if (tick == EventUpdateTick.Update) { _updateSystem.Unsubscribe(target, eventCallback); } else // Late Update { _lateUpdateSystem.Unsubscribe(target, eventCallback); } }
/// <summary> /// Queue an event. /// </summary> /// <param name="target">The target to queue an event with.</param> /// <param name="ev">The event to queue.</param> /// <typeparam name="T_Event">The event type.</typeparam> public void QueueEvent <T_Event>(EventTarget target, T_Event ev) where T_Event : struct { EventHandlerStandard <T_Event> system = GetSystem <T_Event>(); system.QueueEvent(target, ev); List <IEventSystem> list = GetJobSystemsForEvent <T_Event>(); int count = list.Count; for (int i = 0; i < count; i++) { IJobEventSystem <T_Event> typedSystem = (IJobEventSystem <T_Event>)list[i]; typedSystem.QueueEvent(target, ev); } }
/// <summary> /// Send an event to be processed in a specific update tick. /// </summary> /// <param name="target">The target to send the event to.</param> /// <param name="ev">The event to send</param> /// <param name="tick">The update tick to send to.</param> /// <typeparam name="T_Event">The event type.</typeparam> public static void SendEvent <T_Event>(EventTarget target, T_Event ev, EventUpdateTick tick) where T_Event : struct { if (tick == EventUpdateTick.FixedUpdate) { _fixedUpdateSystem.QueueEvent(target, ev); } else if (tick == EventUpdateTick.Update) { _updateSystem.QueueEvent(target, ev); } else // Late Update { _lateUpdateSystem.QueueEvent(target, ev); } }
/// <summary> /// Unsubscribe a job that processed during from an event in the specific update tick. /// </summary> /// <param name="target">The target to unsubscribe from.</param> /// <param name="onComplete">The callback that is invoked when the job has finished.</param> /// <param name="tick">The update type to unsubscribe to.</param> /// <typeparam name="T_Job">The job type.</typeparam> /// <typeparam name="T_Event">The event type.</typeparam> public static void UnsubscribeWithJob <T_Job, T_Event>(EventTarget target, Action <T_Job> onComplete, EventUpdateTick tick) where T_Job : struct, IJobForEvent <T_Event> where T_Event : struct { if (tick == EventUpdateTick.FixedUpdate) { _fixedUpdateSystem.UnsubscribeWithJob <T_Job, T_Event>(target, onComplete); } else if (tick == EventUpdateTick.Update) { _updateSystem.UnsubscribeWithJob <T_Job, T_Event>(target, onComplete); } else // Late Update { _lateUpdateSystem.UnsubscribeWithJob <T_Job, T_Event>(target, onComplete); } }
/// <summary> /// Queue an event to be processed later. /// </summary> /// <param name="target">The entity the event is for.</param> /// <param name="ev">The event to queue.</param> public void QueueEvent(EventTarget target, T_Event ev) { QueuedEvent <T_Event> newEv = new QueuedEvent <T_Event>(target, ev); #if !DISABLE_EVENT_SAFETY_CHKS if (_cachedCurEvents.TryGetValue(newEv, out int index)) { // If avoiding this warning is too annoying then make this the default behaviour and have this be a message sent // when in verbose mode or something Debug.LogWarning( $"To prevent parallel corruption, event {ev.GetType().Name} cannot be sent to the same entity multiple times between processing. This event will replace the previous one!"); _queuedEvents[index] = newEv; } else { _cachedCurEvents[newEv] = _queuedEvents.Length; _queuedEvents.Add(newEv); } #else _queuedEvents.Add(newEv); #endif }
/// <summary> /// Unsubscribe a job from the system. /// </summary> /// <param name="target">The entity to unsubscribe from.</param> /// <param name="onComplete">The callback that was invoked when a job finished.</param> public void Unsubscribe(EventTarget target, Action <T_Job> onComplete) { EntityCallbackId <T_Job> callbackId = new EntityCallbackId <T_Job>(target, onComplete); if (_entityCallbackToIndex.TryGetValue(callbackId, out int index)) { _entityCallbackToIndex.Remove(callbackId); _subscribers.RemoveAtSwapBack(index); _subscriberCallbacks[index] = _subscriberCallbacks[_subscriberCallbacks.Count - 1]; _subscriberCallbacks.RemoveAt(_subscriberCallbacks.Count - 1); if (index != _subscribers.Length) { EntityCallbackId <T_Job> otherCallbackId = new EntityCallbackId <T_Job>( _subscribers[index].target, _subscriberCallbacks[index]); _entityCallbackToIndex[otherCallbackId] = index; } } }
public Subscription(EventTarget target, T_Job job) { this.target = target; this.job = job; }
/// <summary> /// Queue an event to be processed later. /// </summary> /// <param name="target">The target the event is for.</param> /// <param name="ev">The event to queue.</param> public void QueueEvent(EventTarget target, T_Event ev) { _queuedEvents.Add(new QueuedEvent <T_Event>(target, ev)); }
/// <summary> /// Create an tick based event system. /// </summary> /// <param name="updateTick">Which tick to run in.</param> public TickEventSystem(EventUpdateTick updateTick) { _tick = updateTick; _target = EventTarget.CreateTarget(); }
/// <summary> /// Unsubscribe a listener from an event. /// </summary> /// <param name="target">The target to unsubscribe from.</param> /// <param name="eventCallback">The event callback</param> /// <typeparam name="T_Event">The event</typeparam> public void Unsubscribe <T_Event>(EventTarget target, Action <T_Event> eventCallback) where T_Event : struct { EventHandlerStandard <T_Event> system = GetSystem <T_Event>(); system.Unsubscribe(target, eventCallback); }
public static void SendEventUI <T_Event>(this GameObject gObj, T_Event ev) where T_Event : struct { EventManager.SendEvent(EventTarget.CreateTarget(gObj), ev, EventUpdateTick.LateUpdate); }
public static void Subscribe <T_Event>(this GameObject gObj, Action <T_Event> callback) where T_Event : struct { EventManager.Subscribe(EventTarget.CreateTarget(gObj), callback, EventUpdateTick.FixedUpdate); }