Skip to content

Event bus for Unity that emphasizes performance and zero GC allocation (events are structs).

License

Notifications You must be signed in to change notification settings

AnomalousUnderdog/AnnoEventBus

 
 

Repository files navigation

BasicEventBus

Basic event bus for unity

  • Structs for events - no garbage
  • Interface based subscription
  • High performance (1 mil events fired per frame at 60 fps on my machine)

Create an event

Create new struct, assign IEvent interface

public struct TestEvent : IEvent
{
    public string a;
    public float b;
}

Raising an event

Slower method (has to do lookup to resolve subscribers)

EventBus.Raise(new TestEvent()
{
    b = 7,
    a = "Hello"
});

Fast method (directly invoke raise on the generic bus)

EventBus<TestEvent>.Raise(new TestEvent()
{
    b = 7,
    a = "Hello"
});

Subscribing

  1. Implement desired interfaces on a class
public class EventBusTest : MonoBehaviour,
    IEventReceiver<TestEvent>,
    IEventReceiver<OnResourceDrop>
{
  1. Feed instance of it to EventBus.Register()
    void Start()
    {
        EventBus.Register(this);
    }

    private void OnDestroy()
    {
        EventBus.UnRegister(this);
    }

It will automatically subscribe everything, reflection is used only once on application start, after that everything is cached and mapped.

About

Event bus for Unity that emphasizes performance and zero GC allocation (events are structs).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%