Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

FakeMessageBus

A minimal message bus for Unity that is easy to use. FakeMessageBus can form the basis of an message bus or command bus.

Installation

Unity Package Manager

https://github.com/Danila-Che/FakeMessageBus.git?path=/Assets/FakeMessageBus
  1. In Unity, open WindowPackage Manager.
  2. Press the + button, choose "Add package from git URL..."
  3. Enter url above and press Add.

Usage

Full code that uses the message bus (any type can be used, such as value types or reference types):

  1. Declare a message type.
usingSystem;publicclassExampleMessage{ ...}publicstructAthotherExampleMessage{ ...}
  1. Then simply register/unregister an object with callbacks decorated with the ObserveMessage attribute on the message bus.

The callback method must have only one parameter.

usingFakeMessageBus;usingSystem;publicclassExampleObserver:IDisposable{privateMessageBusm_MessageBus;publicExampleObserver(MessageBusmessageBus){m_MessageBus=messageBus;m_MessageBus.Register(this);}publicvoidDispose(){m_MessageBus.Unregister(this);}[ObserveMessage]publicvoidOn(ExampleMessagemessage){
...}[ObserveMessage]publicvoidOn(AthotherExampleMessagemessage){
...}[ObserveMessage]publicvoidOn(intmessage){
...}[ObserveMessage]publicvoidOn(stringmessage){
...}}
  1. At the end, simply send the message using the Send method.
usingFakeMessageBus;publicclassExampleMessageHandler{privateMessageBusm_MessageBus;publicOnRaiseMessage(){m_MessageBus.Send(newExampleMessage(42));}}

MessageBusProxy

MessageBusProxy encapsulates an message bus using the singleton pattern. It has static methods implementing various registration and unregistration strategies for GameObject.

RegisterSingle(GameObject)

RegisterObject(GameObject)

RegisterRecursive(GameObject)

UnregisterSingle(GameObject)

UnregisterObject(GameObject)

UnregisterRecursive(GameObject)

Other static methods:

Clear()

Notify(T)

GameObjectSelfRegistration

Interacts with MessageBusProxy to automatically register and unregister with selected strategy (Single, Object, and Recursive). Register GameObject with the OnEnable callback and uregister GameObject with the OnDisable callback.

Dependency injection

MessageBus implements the IMessageBus interface. I use the Reflex framework to inject MessageBus.

usingReflex.Core;usingUnityEngine;publicclassProjectInstaller:MonoBehaviour,IInstaller{publicvoidInstallBindings(ContainerBuilderbuilder){builder.AddSingleton(typeof(MessageBus),typeof(IMessageBus));}}
usingFakeMessageBus;usingReflex.Core;publicclassExampleObserver:MonoBehaviour{[Inject]privateIMessageBusm_MessageBus;privatevoidOnEnable(){m_MessageBus.Register(this);}privatevoidOnDisable(){m_MessageBus.Unregister(this);}
...}

MessageBus Component

Register(object)

Register an observer with the MessageBus if the observer has valid callbacks. A callback contains only one parameter. The observer will not be registered callback if it contains zero or greater than one parameter.

Exceptions

InvalidCallbackException Observer has at least one callback is invalid.

Unregister(object)

Unregister an observer from MessageBus.

Send(T)

Raises an message with the specified arguments.

Parameter

messageArgs T The message args to be sended to all registered observer callback.

Clear()

Removes all callbacks (observers) from the MessageBus.

GetActiveObserverCount()

Gets the number of registered callback for the specified MessageArgs type.

Return

int The number of registered callback

About

A minimal message bus for Unity that is easy to use. FakeMessageBus can form the basis of an event bus or command bus.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages