Skip to content

EventSystem ‐ Classic

Wagner GFX edited this page Dec 4, 2024 · 3 revisions

The event system uses the argument type to differentiate between each event, calling every listener for that specific type.

EventManager

Subscribe/Unsubscribe listeners and fires events.

All managers prevent duplication when subscribing listeners and errors when unsubscribing them.

⚠ But there is no way to check for dead listeners, so always unsubscribe during OnDestroy() and/or OnDisable()

Object

The core component for every manager, can be used as a standalone object to register listeners and fire events.

Component

Used as a component in a GameObject, it acts as a middle-agent that can fire the event locally to listeners that only registered with it or globally, by calling the Scriptable Object manager.

ScriptableObject

The global manager, can be referenced directly or by component managers.

IEventArgs

The main inteface used to create custom events.

For better performance and good practices, create events using read-only Structs. Only use Classes when really necessary, as they can create a lot of work for the GC system, specially in events called in hot paths.

As another good practice, prefer the use of IEventArgsWithSender to always send a reference of the sender to the listener, it's specially useful for debugging purposes or creating dynamic connections between objects.

Clone this wiki locally