A minimal message bus for Unity that is easy to use. FakeMessageBus can form the basis of an message bus or command bus.
https://github.com/Danila-Che/FakeMessageBus.git?path=/Assets/FakeMessageBus
- In Unity, open Window → Package Manager.
- Press the + button, choose "Add package from git URL..."
- Enter url above and press Add.
Full code that uses the message bus (any type can be used, such as value types or reference types):
- Declare a message type.
using System;
public class ExampleMessage { ... }
public struct AthotherExampleMessage { ... }
- 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.
using FakeMessageBus;
using System;
public class ExampleObserver : IDisposable
{
private MessageBus m_MessageBus;
public ExampleObserver(MessageBus messageBus)
{
m_MessageBus = messageBus;
m_MessageBus.Register(this);
}
public void Dispose()
{
m_MessageBus.Unregister(this);
}
[ObserveMessage]
public void On(ExampleMessage message)
{
...
}
[ObserveMessage]
public void On(AthotherExampleMessage message)
{
...
}
[ObserveMessage]
public void On(int message)
{
...
}
[ObserveMessage]
public void On(string message)
{
...
}
}
- At the end, simply send the message using the
Send
method.
using FakeMessageBus;
public class ExampleMessageHandler
{
private MessageBus m_MessageBus;
public OnRaiseMessage()
{
m_MessageBus.Send(new ExampleMessage(42));
}
}
MessageBusProxy
encapsulates an message bus using the singleton pattern. It has static methods implementing various registration and unregistration strategies for GameObject
.
Other static methods:
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.
MessageBus
implements the IMessageBus
interface. I use the Reflex
framework to inject MessageBus
.
using Reflex.Core;
using UnityEngine;
public class ProjectInstaller : MonoBehaviour, IInstaller
{
public void InstallBindings(ContainerBuilder builder)
{
builder.AddSingleton(typeof(MessageBus), typeof(IMessageBus));
}
}
using FakeMessageBus;
using Reflex.Core;
public class ExampleObserver : MonoBehaviour
{
[Inject] private IMessageBus m_MessageBus;
private void OnEnable()
{
m_MessageBus.Register(this);
}
private void OnDisable()
{
m_MessageBus.Unregister(this);
}
...
}
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.
InvalidCallbackException
Observer has at least one callback is invalid.
Unregister an observer from MessageBus
.
Raises an message with the specified arguments.
messageArgs
T
The message args to be sended to all registered observer callback.
Removes all callbacks (observers) from the MessageBus
.
Gets the number of registered callback for the specified MessageArgs
type.
int The number of registered callback