Coder Social home page Coder Social logo

unibus's Introduction

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

Download Unibus-v1.0.1.unitypackage

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

unibus's People

Contributors

mattak avatar pine avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.