-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleEvents.cs
74 lines (64 loc) · 2.06 KB
/
SimpleEvents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using UnityEngine;
namespace SimpleEvents
{
public class SimpleEvents : MonoBehaviour
{
/// <summary>
/// Singleton instance of SimpleEvents.
/// </summary>
private static SimpleEvents instance;
/// <summary>
/// Receives all SimpleEvents.
/// </summary>
private event Action<string, object[]> onSimpleEvent;
/// <summary>
/// Receives all SimpleEvents.
/// </summary>
public static event Action<string, object[]> onEvent;
private void Awake()
{
// Ensure there is only one instance of SimpleEvents.
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
this.enabled = false;
}
}
/// <summary>
/// Sends a SimpleEvent to all subscribers with any number of data.
/// </summary>
private void SimpleEvent(string eventName, params object[] data)
{
onSimpleEvent?.Invoke(eventName, data);
}
/// <summary>
/// Adds a delegate to the event.
/// </summary>
public static void AddOnSimpleEvent(Action<string, object[]> action)
{
instance.onSimpleEvent += action;
}
/// <summary>
/// Removes a delegate from the event.
/// </summary>
public static void RemoveOnSimpleEvent(Action<string, object[]> action)
{
instance.onSimpleEvent -= action;
}
/// <summary>
/// Sends a SimpleEvent to all subscribers with any number of data.
/// </summary>
public static void Event(string eventName, params object[] data)
{
// First, trigger instance event
instance.SimpleEvent(eventName, data);
// Then, trigger static event
onEvent?.Invoke(eventName, data);
}
}
}