https://github.com/pointcache/BasicEventBus
Basic event bus for unity
https://github.com/pointcache/BasicEventBus
event unity unity3d
Last synced: 6 months ago
JSON representation
Basic event bus for unity
- Host: GitHub
- URL: https://github.com/pointcache/BasicEventBus
- Owner: pointcache
- License: unlicense
- Created: 2017-05-08T03:11:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-14T15:32:41.000Z (almost 2 years ago)
- Last Synced: 2024-11-17T22:36:11.449Z (12 months ago)
- Topics: event, unity, unity3d
- Language: C#
- Size: 38.1 KB
- Stars: 87
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-unity-open-source-on-github - BasicEventBus - commit/pointcache/BasicEventBus?logoSize=auto) - Basic event bus (Message Bus)
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;
}
```