Skip to content

jonanh/Elurnity.EventSystem

Repository files navigation

Elurnity.EventSystem Build Status

Event system for Unity3D

Features

  • Strongly typed events.
  • Event bubbling in Unity scenes.

API

Events

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.

Event handler

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;

Listener

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.

Work in progress

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.

Inspired by

References

Alternatives

About

Event system for Unity3D

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages