-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathExampleSimple.cs
72 lines (58 loc) · 2.29 KB
/
ExampleSimple.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
using UnityEngine;
using UnityEvents.Internal;
namespace UnityEvents.Example
{
/// <summary>
/// Simple example of using the global and GameObject event systems
/// </summary>
public class ExampleSimple : MonoBehaviour
{
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
private struct EvExampleEvent
{
public int exampleValue;
public EvExampleEvent(int exampleValue)
{
this.exampleValue = exampleValue;
}
}
private void OnEnable()
{
// Subscribes to the global event system, handles events in FixedUpdate
GlobalEventSystem.Subscribe<EvExampleEvent>(OnExampleEvent);
// Subscribes to THIS GameObject's event system! Also Fixed Update
gameObject.Subscribe<EvExampleEvent>(OnExampleEvent);
// Is the game paused but still need events for UI? There's a global UI system. Handles events in
// LateUpdate
GlobalEventSystem.SubscribeUI<EvExampleEvent>(OnExampleEvent);
// There's also local event system for each GameObject that run in LateUpdate.
gameObject.SubscribeUI<EvExampleEvent>(OnExampleEvent);
}
private void OnDisable()
{
// Should always unsubscribe
// Unsubscribe from the global system
GlobalEventSystem.Unsubscribe<EvExampleEvent>(OnExampleEvent);
gameObject.Unsubscribe<EvExampleEvent>(OnExampleEvent);
GlobalEventSystem.UnsubscribeUI<EvExampleEvent>(OnExampleEvent);
gameObject.UnsubscribeUI<EvExampleEvent>(OnExampleEvent);
}
public void SendEvents()
{
// Send an event to the global event system, will be processed in the next FixedUpdate
GlobalEventSystem.SendEvent(new EvExampleEvent(10));
// Send an event to a specific GameObject, only listeners subscribed to that gameobject will get
// this event. Also will be processed in the next FixedUpdate
gameObject.SendEvent(new EvExampleEvent(99));
// Can send events to the global UI event system. These will be processed in LateUpdate which allows the
// game to paused.
GlobalEventSystem.SendEventUI(new EvExampleEvent(-1));
// Similarly can send to a specific GameObject to be processed in LateUpdate
gameObject.SendEventUI(new EvExampleEvent(999999));
}
private void OnExampleEvent(EvExampleEvent ev)
{
Debug.Log("Event received! Value: " + ev.exampleValue);
}
}
}