Home

Awesome

KLab Message Buses for Unity

While not silver bullets, message buses (or event buses as Lumberyard calls them) can be an effective mechanism for loosen up coupling between systems. KLab Message Buses for Unity provides a simple API for using buses in Unity with low runtime overhead.

Usage

Using a message bus basically consists of 3 steps.

  1. You declare a message bus
  2. You connect to it (and later disconnect from it)
  3. You send messages through the bus

The below is a minimal example. (The example uses Unity components for handling connecting and sending, but you can also use message buses in pure C# classes).

using KLab.MessageBuses;
using UnityEngine;


public sealed class MyMessageBus : MessageBus<string> {}


public sealed class Sender : MonoBehaviour
{
    private const string Message = "Hello, World!";

    private MyMessageBus Bus { get; set; }



    private void Start ()
    {
        Bus = MessageBus.GetBus<MyMessageBus>();
    }

    private void Update ()
    {
        Bus.Broadcast(Message);
    }
}


public sealed class Receiver : MonoBehaviour
{
    private void OnMessage(string message)
    {
        Debug.Log(message);
    }


    private void OnEnable()
    {
        MessageBus
            .GetBus<MyMessageBus>()
            .Connect(OnMessage);
    }

    private void OnDisable()
    {
        MessageBus
            .GetBus<MyMessageBus>()
            .Disconnect(OnMessage);
    }
}

See here for bus types available.

Advanced Usage

Setting Initial Container Capacities

You can set initial capacities of the underlying containers used by a bus by decorating the bus with options.

using KLab.MessageBuses;


[MessageBusOptions(connectionsCapacity : 1024)]
public sealed class MyMessageBus : MessageBus<string> {}

Finding All Connection Propertiess For Later Query

See here.

Finding All Waive Methods For Later Invokation

See here.

Creating A Non-global Bus

If you want to create a non-global message bus, simply instantiate the message bus class directly instead of getting the global singleton through MessageBus.GetBus<T>().

Unity Import

The library can be imported easily as a Unity package. It doesn't have any dependencies on other packages.

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[1.1.0] - 2019-05-09

Added

Changed

Feedback

License

Copyright (c) KLab Inc.. All rights reserved.

Licensed under the MIT License.