Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

BasicEventBus

Basic event bus for unity

  • Simple
  • Structs for events - no garbage
  • Binding based or direct subscription, no interfaces
  • High performance

Create an event

Create new struct, assign IEvent interface

publicstructTestEvent:IEvent{publicstringa;publicfloatb;}

Raising an event

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

Usage

Check EventBusExample.cs

  1. Declare an event binding object
publicclassEventBusTest:MonoBehaviour{EventBinding<TestEvent>_onTestEvent;
  1. Initialize it in Awake/Start/Ctor
privatevoidAwake(){_onTestEvent=newEventBinding<TestEvent>(OnEvent);// Add couple more listeners to the same binding _onTestEvent.Add(onEventButPrintTheStringInstead);_onTestEvent.Add(someUnrelatedMethodWithoutArgs);EventBus<TestEvent>.Raise(newTestEvent(){a="Hello"});_onTestEvent.Remove(onEventButPrintTheStringInstead);_onTestEvent.Remove(someUnrelatedMethodWithoutArgs);// Callback will be called precisely once, then droppedEventBus<TestEvent>.AddCallback(SomeRandomMethod);StartCoroutine(CoroutineThatDispatchesEventAfterSomeTime());StartCoroutine(CoroutineThatWaitsForEvent());}
  1. Control observing state through Listen
privatevoidOnEnable(){_onTestEvent.Listen=true;}privatevoidOnDisable(){_onTestEvent.Listen=false;}

About

Basic event bus for unity

Topics

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages