UniSignal is a flexible and performance-focused C# signal management library, tailored for Unity. It supports subscribing and dispatching signals based on class type, specific data, or custom predicates.
Open the Package Manager in Unity, click on "+" button, choose "Add package from git URL..." and paste:
https://github.com/actionk/UniSignal.git#0.1
In order to start using UniSignal, you need an instance of SignalHub.
var signalHub = new SignalHub();
One can subscribe to signals by their type and the callback will be called for any signal of this type.
signalHub.Subscribe<TestSignal>(this, () => Debug.Log("Received signal"));
When subscribing to a signal, you have the option to pass an object as the listener ("this" in that case). This object represents the owner of the subscription. It's useful for when you want to unsubscribe all signals tied to a specific object, for example when an object is destroyed or no longer needs to listen for signals.
In this case, one should inherit from ISignal<>
, which implements IEquatable<T>
to check if that's exactly the signal we're waiting for.
signalHub.Subscribe(this, new TestSignalWithData(5), () => Debug.Log("Received specific signal"));
In this case, the callback will only be called for signals of the specified type that are valid according to the predicate.
signalHub.Subscribe<TestSignalWithData>(this, signal => signal.testData == 5, () => Debug.Log("Received signal with predicate"));
var subscription = signalHub.Subscribe<TestSignal>(this, () => Debug.Log("Received signal"));
signalHub.Unsubscribe(subscription);
signalHub.Subscribe<TestSignal>(this, () => Debug.Log("Received signal"));
signalHub.Unsubscribe(this);
Please refer to the SignalsTest.cs
file for examples and test cases.
Any contributions you make are greatly appreciated.
Distributed under the MIT License. See LICENSE
for more information.