mattak / Unibus

Unibus is event passing system for Unity3D. πŸš€

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unibus

Unibus

Unibus is event passing system for Unity3D.

It is inspired by EventBus.

Why Unibus?

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.

Architecture

Unibus

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

Install from UPM

{
  "dependencies": {
    "me.mattak.unibus": "https://github.com/mattak/Unibus.git?path=Assets/Plugins/Unibus"
   }
}

Usage

1. Place Unibus object

Place Unibus prefab object into your scene. It will be singleton to handle event.

Place Unibus prefab

Then it's ready to use.

2. Implement event sender

Send any event what you want.

using UnibusEvent;

public class SampleEventSender : MonoBehaviour
{
    void OnClick()
    {
        // Send string message
        Unibus.Dispatch("message");
    }
}

3. Implement event receiver

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; });
    }
}

Sending Object

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"));

Tagging

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);

License

MIT

About

Unibus is event passing system for Unity3D. πŸš€

License:MIT License


Languages

Language:C# 100.0%