Skip to content

🔌 Fast EventBus realization for Unity with Coroutine support

License

Notifications You must be signed in to change notification settings

aleverdes/signals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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")
}

About

🔌 Fast EventBus realization for Unity with Coroutine support

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages