Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pointcache/BasicEventBus

Basic event bus for unity
https://github.com/pointcache/BasicEventBus

event unity unity3d

Last synced: 3 months ago
JSON representation

Basic event bus for unity

Awesome Lists containing this project

README

        

# 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

```cs

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

}
```

# Raising an event

```cs

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

```

# Usage

Check EventBusExample.cs

1. Declare an event binding object

```cs

public class EventBusTest : MonoBehaviour
{
EventBinding _onTestEvent;

```

2. Initialize it in Awake/Start/Ctor

```cs

private void Awake()
{
_onTestEvent = new EventBinding(OnEvent);
// Add couple more listeners to the same binding
_onTestEvent.Add(onEventButPrintTheStringInstead);
_onTestEvent.Add(someUnrelatedMethodWithoutArgs);

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

_onTestEvent.Remove(onEventButPrintTheStringInstead);
_onTestEvent.Remove(someUnrelatedMethodWithoutArgs);

// Callback will be called precisely once, then dropped
EventBus.AddCallback(SomeRandomMethod);

StartCoroutine(CoroutineThatDispatchesEventAfterSomeTime());
StartCoroutine(CoroutineThatWaitsForEvent());
}
```

3. Control observing state through `Listen`

```cs

private void OnEnable()
{
_onTestEvent.Listen = true;
}

private void OnDisable()
{
_onTestEvent.Listen = false;
}
```