Event system for Unity3D
- Strongly typed events.
- Event bubbling in Unity scenes.
namespace Elurnity.EventSystem;
public struct TestEvent : Event
{
public string field;
}
Events are structs implementing the Event interface.
The Event interface allows us to inspect all the events in the system using reflection.
Since they are value types, the performance impact of emiting events is low.
Action<Event> handler = (Event evt) => Console.Write(evt.ToString());
public class Receiver
{
public void OnEventReceived(Event evt)
{
Console.Write(evt.ToString()
}
}
Action<Event> handler = new Receiver().OnEventReceived;
public sealed class EventListener
{
public void On<T>(Action<T> listener) where T : struct, Event;
public void On<T>(Action<T> listener, EventListener to) where T : struct, Event;
public void Off<T>(Action<T> listener) where T : struct, Event;
public void Off<T>(Action<T> listener, EventListener to) where T : struct, Event;
public void Emit<T>(T evt) where T : struct, Event;
The listener delivers the emitted event into the handlers registered into it. It also allows to listen to the events emitted into another listener.
In Unity the listener class in instantiated dynamically for each game object.
- Event bubbling through a pseudo DOM.
A chain of listeners can be constructed.
- Fast and memory leak safe by using cached open delegates and weak references.
Open handler
public class Receiver
{
public void OnEventReceived(Event evt)
{
Console.Write(evt + " received by " + instance);
}
}
Action<Receiver, Event> handler = Receiver.OnEventReceived;
Open handlers as opposed to closed handlers don't have an implicit reference to the receiver, allowing a weak reference to be stored pointing to the Receiver instance and sharing the same delegate handler for all the Receivers.
- API inspired by node.js's events
- Javascript bubbling backbone's listenTo
- Angularjs's event scope bubbling.