Unity Game Engine Event Management System
- global events with Event or events local to a context with ContextEvent
- no need to remove listeners in OnDestroy for MonoBehaviours
- event queue: all listeners of the original event are executed before any listener of a nested event
- listener predicate conditions
- 3 priority queues per event type
- easy access to the last event of any type
Install via Add package from git URL in the Package Manager.
Create a separate class for each event:
public class ExampleEvent : Aether.Event<ExampleEvent>
{
public readonly int Variable;
public ExampleEvent(int variable)
{
Variable = variable;
}
}
Add listeners and remove when not needed anymore (they will be automatically removed when the game object gets destroyed):
public class ExampleListener : MonoBehaviour
{
private void Awake()
{
ExampleEvent.AddListener(OnEvent);
}
private void OnEvent(ExampleEvent eventData)
{
Debug.Log(eventData.Variable);
ExampleEvent.RemoveListener(OnEvent);
}
}
Create a new event instance and invoke the event:
public class ExampleInvoker : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
new ExampleEvent(0).Invoke();
}
}
Use readonly fields or private setter properties as event parameters to prevent listeners from changing them.
Use Awake
to subscribe to events whenever possible and don't invoke any events until Start
to be sure that all listeners have already subscribed.
If you invoke a new event from an event listener, the original event will execute on all of the remaining listeners first.
Use Last property to access the last invoked event of a given type (can be null if the event hasn't been invoked yet).
Inherit from ContextEvent
instead and pass an additional context parameter when adding a listener and invoking the event. Only listeners with the same context as the event will be executed.
Pass an additional priority argument when adding a listener to invoke that listener pre or post other listeners for that event.
Use predicate field when adding a listener to only invoke the listener if a condition is met.
Subscribe to EventProcessed event to handle all events of any type.