Home

Awesome

Signals ❇

Signals Editor Window Screenshot

A typesafe, lightweight, tested messaging package for Unity.

openupm Unity 5.6 or later Tested up to Unity 2020.2

Installation

Install the package with OpenUPM

$ openupm add com.supyrb.signals

or download the Latest Unity Packages

Features

As well as

Usage

BasicExample

Get Signal

BasicExampleSignal exampleSignal;

// Get the signal by passing its variable
Signals.Get(out exampleSignal);
// or with a generic get
exampleSignal = Signals.Get<BasicExampleSignal>();
// or by passing the type
exampleSignal = Signals.Get(typeof(BasicExampleSignal));

Subscribe to Signal

//if you didn't store the Signal in a variable you can use
Signals.Get<BasicExampleSignal>().AddListener(DefaultListener);

// Default subscription with order 0
exampleSignal.AddListener(DefaultListener);

// Subscribe with an earlier order to be called first
exampleSignal.AddListener(FirstListener, -100);

Unsubscribe to Signal

//if you didn't store the Signal in a variable you can use
Signals.Get<BasicExampleSignal>().RemoveListener(DefaultListener);

// Unsubscribe does not care for the listening order
exampleSignal.RemoveListener(DefaultListener);

Dispatch Signal

// Send the signal to all listeners (if not consumed or paused in between)
exampleSignal.Dispatch();

// If your type has a parameter, is is added in the dispatch method
Signals.Get<GameObjectSignal>().Dispatch(gameObject);

Pause & Continue

// No more listeners will be called until the signal is continued
exampleSignal.Pause();

// Will continue after the listener that paused the signal
exampleSignal.Continue();

If you want to pause the further propagation of a signal (wait for a user input/scene that needs to load/network package) you can easily do that with signal.Pause() and signal.Continue().

Consume Signals

// No more listeners will receive this signal
exampleSignal.Consume();

Sometimes only one script should handle a signal or the signal should not reach others. Unity for example does this with keystrokes in the editor, you can decide in the script if the event is used. Similar to that, you can consume signals with signal.Consume(). Always be away of the order of your listeners. Listeners with a lower order value are called first and therefore decide before others if they should get the event as well.

Editor Window

The editor window can be accessed through Window->Signals->Singals. On the first start and whenever you added a signal you want to debug, just hit the refresh button in the bottom right corner of the window

Detail View

Caveats

Contribute

Contributions to the repository are always welcome. There are several ways to contribute:

Code Contribution

Setup

  1. Create a new Unity Project
  2. Clone git repository in your Assets folder C:\UnityProject\Assets> git clone https://github.com/supyrb/signals.git
  3. Get access to the samples
    1. Simple way: Copy folder UnityProject/Assets/Signals/Samples~ to UnityProject/Assets/SignalSamples in order to see/use the examples. Whenever you make changes in the new folder, you would manually need to copy them to the ~Samples folder again.
    2. Hard way: Run a command prompt as admin and go to the Assets folder of the project, then run mklink /D SignalSamples .\signals\Samples~. This will allow you to see the otherwise hidden folder and changes done to the samples with show up as changes in git directly.

Guidelines

Credits

License

💥Supyrb