Signal Management

Actions that listen for signals are kept in SignalBus. When the relevant signal is fired, listening actions are triggered. In this way, there is no dependency between the place that fires the signal and the place that listens.

Sample uses:

public class Installer : GameObjectInstaller
{
    public override void Install(DIContainer container)
    {
        container.Bind<SignalController>();
    }
}

public readonly struct FooSignal
{
    public readonly string Data;

    public FooSignal(data)
    {
        Data = data;
    }
}

public class SampleClass : MonoInjecter
{
    [Inject]
    private readonly SignalController _signalController;

    protected override void Awake()
    {
        base.Awake();

        SubscribeEvents();
    }

    private void SubscribeEvents()
    {
        _signalController.Subscribe<FooSignal>(OnFoo);
    }

    private void OnFoo(string signal)
    {
        Debug.Log($"Signal Received - string: {signal.Data}");
    }

    private void UnsubscribeEvents()
    {
        _signalController.Unsubscribe<UserDataReceivedSignal>(OnUserDataReceived);
    }

    private void OnDestroy()
    {
        UnsubscribeEvents();
    }
}

public class FooItem : MonoInjecter
{
    [Inject]
    private readonly SignalController _signalController;

    protected override void Awake()
    {
        base.Awake();

        _signalController.Fire(new FooSignal("Sample string from {gameObject.name]"));
    }
}

Last updated