Unibus is event passing system for Unity3D.
It is inspired by EventBus.
Unity is great framework for creating game, but there is no standard event passing system.
For example in GameJam, there is no time to solve GameObject dependencies and create so many callback.
So I create instant event passing system.
It's easy to use, thin dependency, flexible to fit any type of message.
Unibus is singleton GameObject.
It manages receivers and senders to handle message.
The message is classified by tag and type.
For example, if you dispatch message with HP
tag and int
type, then receivers subscribing HP
tag and int
type can only receives the dispatched value.
Install from UPM
{
"dependencies": {
"me.mattak.unibus": "https://github.com/mattak/Unibus.git?path=Assets/Plugins/Unibus"
}
}
Place Unibus
prefab object into your scene.
It will be singleton to handle event.
Then it's ready to use.
Send any event what you want.
using UnibusEvent;
public class SampleEventSender : MonoBehaviour
{
void OnClick()
{
// Send string message
Unibus.Dispatch("message");
}
}
Receive sent message.
using UnibusEvent;
public class SampleEventReceiver : MonoBehavour
{
void OnEnable()
{
Unibus.Subscribe<string>(OnEvent);
}
void OnDisable()
{
Unibus.Unsubscribe<string>(OnEvent);
}
// This is receiver
void OnEvent(string message)
{
var text = this.GetComponent<Text>();
text.text = message;
}
}
Or you can use simple style subscriber.
BindUntilDisable()
is shortcut to unsubscribe automatically when gameobject reach onDisable()
.
using UnibusEvent;
public class SampleEventReceiver : MonoBehavour
{
void OnEnable()
{
this.BindUntilDisable((string message) => { this.GetComponent<Text>().text = message; });
}
}
It's able to send any type of object.
// Subscribe
this.BindUntilDisable((int value) => {});
this.BindUntilDisable((string value) => {});
this.BindUntilDisable((Person value) => {});
// Dispatch
Unibus.Dispatch(0);
Unibus.Dispatch("message");
Unibus.Dispatch(new Person("john", "due"));
Divide same type of object event by attaching tag.
// Subscribe
this.BindUntilDisable("HP", (int value) => {});
this.BindUntilDisable("MP", (int value) => {});
// Dispatch
Unibus.Dispatch("HP", 100);
Unibus.Dispatch("MP", 200);