Yet another event bus C# implementation, for Unity. Heavily inspired on GameEventBus, by ThomasKomarnicki.
Go to the Unity Package Manager (Window > Package Manager
), open the +
dropdown on the top-left corner and select Add package from git URL...
, and provide the following git URL:
git@github.com:Molotov-Studios/SimpleEventBus.git
To use this library properly, you should:
- Create your custom events by implementing the
SimpleEventBus.IEvent
interface - Either create a
SimpleEventBus.EventBus
instance or useSimpleEventBus.EventBus.global
- Subscribe and unsubscribe to events using its
Subscribe/Unsubscribe
methods - Publish events by using the
Publish
method
For example, using the static SimpleEventBus.EventBus.global
default bus:
// Create an event by implementing the SimpleEventBus.IEvent interface
public class HelloEvent : SimpleEventBus.IEvent
{
// You can use any kind of parameters and/or methods
public HelloEvent(int firstParameter, string secondParameter) { }
}
// Example of a publisher
public class HelloPublisher : MonoBehaviour
{
private void Update()
{
// Check your condition
SimpleEventBus.EventBus.global.Publish<HelloEvent>(new HelloEvent(42, "hi!"));
}
}
// Example of a subscriber
public class HelloSubscriber : MonoBehaviour
{
private void OnEnable()
{
SimpleEventBus.EventBus.global.Subscribe<HelloEvent>(OnHelloEvent);
}
private void OnDisable()
{
SimpleEventBus.EventBus.global.Unsubscribe<HelloEvent>(OnHelloEvent);
}
private void OnHelloEvent(HelloEvent e)
{
Debug.Log("Received HelloEvent!");
}
}
With a local SimpleEventBus.EventBus
instance:
using SimpleEventBus;
// Example of a publisher
public class ClosedSystem
{
private class HelloEvent : IEvent
{
public HelloEvent() { }
}
private void CustomMethod()
{
EventBus bus = new EventBus();
bus.Subscribe<HelloEvent>(OnHelloEvent);
bus.Publish<HelloEvent>(new HelloEvent());
}
private void OnHelloEvent(HelloEvent e)
{
// Handle event
}
}