Skip to content

Commit b1af1f4

Browse files
authored
Update README.md
1 parent 8cd7ec4 commit b1af1f4

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

README.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ Import Signals.unitypackage to your Unity Project
1919

2020
### How to create a signal
2121

22-
Just create a plain C# class.
22+
Just create a plain C# struct and implement `ISignal` interface.
2323

2424
```csharp
25-
public class SignalPlayerCreated
25+
public readonly struct SignalPlayerCreated : ISignal
2626
{
27-
public string Name { get; }
28-
public GameObject StartWeapon { get; }
27+
public readonly string Name;
28+
public readonly GameObject StartWeapon;
2929

3030
public SignalPlayerCreated(string name, GameObject startWeapon)
3131
{
@@ -37,7 +37,7 @@ public class SignalPlayerCreated
3737

3838
### How to send a signal
3939

40-
You need to call `Hub.Dispatch` and pass in instance of signal.
40+
You need to call `Hub<S>.Dispatch(new S())`. Instead of `S` you need to pass in type of your signal.
4141

4242
```csharp
4343
public class Player : MonoBehaviour
@@ -54,24 +54,31 @@ public class Player : MonoBehaviour
5454

5555
### How to receive a signal
5656

57-
Your class/struct should implement `IReceiver<T>` interface. Pass in Signal instead of T.
57+
Your class/struct must implement `IReceiver<S>` interface. Instead of `S` you need to pass in type of your signal.
5858

5959
```csharp
6060
public class PlayerUI : MonoBehaviour, IReceiver<SignalPlayerCreated>
6161
{
6262
[SerializeField] private TMP_Text _playerName = null;
6363
[SerializeField] private Image _weaponIcon = null;
6464

65+
private void Awake()
66+
{
67+
// You can get last dispatched signal like this
68+
// This is useful if for example UI was created later then the player
69+
Receive(Hub<SignalPlayerCreated>.Last);
70+
}
71+
6572
private void OnEnable()
6673
{
6774
// Subscribing to signal
68-
Hub.Add<SignalPlayerCreated>(this);
75+
Hub<SignalPlayerCreated>.Add(this);
6976
}
7077

7178
private void OnDisable()
7279
{
7380
// Unsubscribing from signal
74-
Hub.Remove<SignalPlayerCreated>(this);
81+
Hub<SignalPlayerCreated>.Remove(this);
7582
}
7683

7784
// Receiving the signal
@@ -85,7 +92,7 @@ public class PlayerUI : MonoBehaviour, IReceiver<SignalPlayerCreated>
8592

8693
### UnityEvent
8794

88-
Create a class that inherits from Listener<```SignalType```> and attach it to any game object you want. Now you can invoke UnityEvent to corresponding Signal.
95+
Create a class that inherits from `Listener<SignalType>` and attach it to any game object you want. Now you can invoke UnityEvent to corresponding Signal.
8996

9097
```csharp
9198
public class PlayerCreatedListener : Listener<SignalPlayerCreated> { }

0 commit comments

Comments
 (0)