Skip to content

Latest commit

 

History

History
80 lines (62 loc) · 1.54 KB

README.md

File metadata and controls

80 lines (62 loc) · 1.54 KB

AleVerDes.Signals

Simple EventBus-pattern implementation for Unity with Coroutine support.


A simple library for working with the message bus. Supports a CustomYieldInstruction for coroutines.

Inspired by yankooliveira/Signals.

Usage

Define signals:

public struct PlayParticlesSignal
{
    public string Name;
    public int Count;
}

public struct DebugSignal
{
}

public struct ChatMessageSignal
{
    public string Text;
}

Signal subscription:

[Inject] private readonly SignalBus _signalBus;
[Inject] private readonly KeySignalBus<string> _idSignalBus;

void Start()
{
    _signalBus.Subscribe<PlayParticlesSignal>(OnPlayParticlesSignal);
    _idSignalBus.Subscribe<PlayParticlesSignal>("invokedOnlyForThisId", OnPlayParticlesSignal);
}

void OnDestroy()
{
    _signalBus.Unsubscribe<PlayParticlesSignal>(OnPlayParticlesSignal);
    _idSignalBus.Unsubscribe<PlayParticlesSignal>("invokedOnlyForThisId", OnPlayParticlesSignal);
}

void OnPlayParticlesSignal(PlayParticlesSignal signal)
{
    // play animation
}

Signal raise:

_signalBus.Invoke(new()
{
    Text = "test"
});

_idSignalBus.Invoke("invokedOnlyForThisId", new()
{
    Text = "test"
});

Coroutine usage

private IEnumerator Start()
{
    Debug.Log("Start-coroutine started");
    
    yield return new WaitForSignal<WinSignal>();
    yield return new WaitForKeySignal<WinSignal>("invokedOnlyForThisId");
    
    Debug.Log("Next frame after WinSignal was invoked")
}