Class MessageRelay<TMessage>
Bridge between the event-aggregator bus and Unity's inspector-wired event system, allowing
TMessage arrivals to drive any UnityEvent target without writing code.
public abstract class MessageRelay<TMessage> : MonoBehaviour, IHandle<TMessage> where TMessage : IMessage
Type Parameters
| Name | Type | Description |
|---|---|---|
TMessage | The type of message to subscribe to and relay. |
Inheritance
Implements
Examples
// A concrete relay that bridges BatteryMessage to a UnityEvent wired in the Inspector:
public class BatteryMessageRelay : MessageRelay
{
[SerializeField] private UnityEvent OnBatteryLevelChanged;
public override void Handle(BatteryMessage msg) => OnBatteryLevelChanged?.Invoke(msg.level);
}
Remarks
Unity's built-in components (sliders, text labels, animators, …) expose UnityEvents that can
be wired in the Inspector, but they cannot directly implement IHandle.
MessageRelay fills the gap: a small adapter MonoBehaviour that subscribes to the
aggregator, receives the typed message, and fires a UnityEvent that the built-in component's
callback can be connected to via drag-and-drop in the Inspector.
Subclasses are abstract because the mapping from TMessage fields to
UnityEvent arguments is message-specific. A BatteryMessageRelay, for example, would
extract msg.level and pass it to a UnityEvent. This design keeps
the base class completely generic while letting each concrete relay own its own UnityEvent
declaration and Inspector layout.
Fields
m_ChannelKey
Channel this relay listens on; null subscribes to the global default channel.
[SerializeField]
protected ChannelKey m_ChannelKey
Field Value
Remarks
Must match the channel used by the publisher. In multi-vehicle scenes, assign each relay to its vehicle's own ChannelKey asset so relays only respond to messages from the vehicle they are attached to.
Methods
Handle(TMessage)
Receives the TMessage from the aggregator and converts it into the
appropriate UnityEvent invocation.
public abstract void Handle(TMessage message)
Parameters
| Name | Type | Description |
|---|---|---|
message | TMessage | The message payload delivered by the aggregator. |
Remarks
Subclasses extract the relevant fields from message and pass them to
their declared UnityEvent. For example, a battery relay would call
OnBatteryLevelChanged?.Invoke(msg.level).
OnDisable()
Unsubscribes from the IEventAggregator when this relay is deactivated.
protected virtual void OnDisable()
OnEnable()
Subscribes to the IEventAggregator when this relay becomes active.
protected virtual void OnEnable()
Remarks
virtual so subclasses can subscribe to additional channels or perform extra setup
before or after calling base.OnEnable().