Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/intothedev/signals
Lightweight type-safe messaging system
https://github.com/intothedev/signals
events signals unity unity-scripts unity2d unity3d
Last synced: 4 months ago
JSON representation
Lightweight type-safe messaging system
- Host: GitHub
- URL: https://github.com/intothedev/signals
- Owner: IntoTheDev
- License: mit
- Created: 2019-08-31T13:10:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-09T20:41:37.000Z (over 3 years ago)
- Last Synced: 2024-10-10T10:24:09.203Z (4 months ago)
- Topics: events, signals, unity, unity-scripts, unity2d, unity3d
- Language: C#
- Homepage:
- Size: 135 KB
- Stars: 55
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# Signals
### TODO
- [ ] Abstract signalsUpdate: With my current knowledge I can't implement it without huge performance loss
## Features
- Type-safe
- Very fast in terms of performance
- Allows you to get last dispatched signal## How to Install
### Git Installation (Best way to get latest version)If you have Git on your computer, you can open Package Manager indside Unity, select "Add package from Git url...", and paste link ```https://github.com/IntoTheDev/Signals.git```
or
Open the manifest.json file of your Unity project.
Add ```"com.intothedev.signals": "https://github.com/IntoTheDev/Signals.git"```### Manual Installation (Version can be outdated)
Download latest package from the Release section.
Import Signals.unitypackage to your Unity Project## Usage
### How to create a signal
Create a plain C# class or struct. I prefer to use readonly structs.
```csharp
public readonly struct PlayerCreated
{
public readonly string Name;
public readonly GameObject StartWeapon;public PlayerCreated(string name, GameObject startWeapon)
{
Name = name;
StartWeapon = startWeapon;
}
}
```### How to send a signal
You need to call `Signal.Dispatch(new S())`. Instead of `S` you need to pass in type of your signal.
```csharp
public class Player : MonoBehaviour
{
[SerializeField] private string _name = "";
[SerializeField] private GameObject _weapon = null;private void Start()
{
Signal.Dispatch(new PlayerCreated(_name, _weapon));
}
}
```### How to receive a signal
Your class/struct must implement `IReceiver` interface. Instead of `S` you need to pass in type of your signal.
```csharp
public class PlayerUI : MonoBehaviour, IReceiver
{
[SerializeField] private TMP_Text _playerName = null;
[SerializeField] private Image _weaponIcon = null;private void Awake()
{
// You can get last dispatched signal like this
// This is useful if for example UI was created later then the player
if (Signal.TryGetLast(out var last))
Receive(last);
}private void OnEnable()
{
// Subscribing to signal
Signal.AddReceiver(this);
}private void OnDisable()
{
// Unsubscribing from signal
Signal.RemoveReceiver(this);
}// Receiving the signal
public void Receive(in PlayerCreated value)
{
_playerName.text = value.Name;
_weaponIcon.sprite = value.StartWeapon.GetComponent().Icon;
}
}
```### UnityEvent
Create a class that inherits from `Listener` and attach it to any game object you want. Now you can invoke UnityEvent to corresponding Signal.
```csharp
public class PlayerCreatedListener : Listener { }
```