actionk / UniSignal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UniSignal Github license Unity 2020 GitHub package.json version

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.

Table of Contents

Getting Started

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

Usage

SignalHub

In order to start using UniSignal, you need an instance of SignalHub.

var signalHub = new SignalHub();

Subscribing to Empty Signals

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.

Subscribing to Signals with Specific Content

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

Subscribing to Signals with a Predicate

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

Unsubscribing

Unsubscribing from Specific Subscription

var subscription = signalHub.Subscribe<TestSignal>(this, () => Debug.Log("Received signal"));
signalHub.Unsubscribe(subscription);

Unsubscribing from All Subscriptions of This Listener

signalHub.Subscribe<TestSignal>(this, () => Debug.Log("Received signal"));
signalHub.Unsubscribe(this);

Tests and Examples

Please refer to the SignalsTest.cs file for examples and test cases.

Contributing

Any contributions you make are greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

About

License:MIT License


Languages

Language:C# 100.0%