|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using UnityEngine;
|
5 | 5 | using UnityEngine.Events;
|
| 6 | +using UnityEngine.Serialization; |
6 | 7 |
|
7 | 8 | namespace Observer.DifferentEvents
|
8 | 9 | {
|
@@ -15,19 +16,19 @@ public class DifferentEventAlternatives : MonoBehaviour
|
15 | 16 |
|
16 | 17 | //C# built-in EventHandler
|
17 | 18 | //Requires "using System;"
|
18 |
| -public event EventHandler MyCoolEvent; |
| 19 | +public event EventHandler myCoolEvent; |
19 | 20 | //With parameters
|
20 |
| -public event EventHandler<MyName> MyCoolEventWithParameters; |
| 21 | +public event EventHandler<MyName> myCoolEventWithParameters; |
21 | 22 |
|
22 | 23 |
|
23 | 24 | //C# built-in Action
|
24 | 25 | //If we have more parameters we can use Action. Compared with EventHandler, the parameters dont have to inherit from EventArgs
|
25 |
| -public event Action<MyName, MyAge> MyCoolEventAction; |
| 26 | +public event Action<MyName, MyAge> myCoolEventAction; |
26 | 27 |
|
27 | 28 |
|
28 | 29 | //Unity built-in UnityEvent
|
29 | 30 | //Requires that we are "using UnityEngine.Events;"
|
30 |
| -public UnityEvent CoolUnityEvent = new UnityEvent(); |
| 31 | +[FormerlySerializedAs("CoolUnityEvent")] public UnityEvent coolUnityEvent = new UnityEvent(); |
31 | 32 | //If you have parameters you have to create a new event class that inherits from UnityEvent<parameter1, parameter2, ...>
|
32 | 33 | public MyCustomUnityEvent coolCustomUnityEvent = new MyCustomUnityEvent();
|
33 | 34 | //There's also something called UnityAction
|
@@ -55,23 +56,23 @@ public class DifferentEventAlternatives : MonoBehaviour
|
55 | 56 | public delegate void MyEventHandlerEmpty();
|
56 | 57 |
|
57 | 58 | //The event belonging to the custom delegate
|
58 |
| -public event MyEventHandlerEmpty MyCoolCustomEvent; |
| 59 | +public event MyEventHandlerEmpty myCoolCustomEvent; |
59 | 60 |
|
60 | 61 |
|
61 | 62 |
|
62 | 63 | void Start()
|
63 | 64 | {
|
64 |
| -MyCoolEvent += DisplayStuff; |
| 65 | +myCoolEvent += DisplayStuff; |
65 | 66 |
|
66 |
| -MyCoolEventWithParameters += DisplayStuffCustomArgs; |
| 67 | +myCoolEventWithParameters += DisplayStuffCustomArgs; |
67 | 68 |
|
68 |
| -MyCoolEventAction += DisplayStuffCustomParameters; |
| 69 | +myCoolEventAction += DisplayStuffCustomParameters; |
69 | 70 |
|
70 |
| -CoolUnityEvent.AddListener(DisplayStuffEmpty); |
| 71 | +coolUnityEvent.AddListener(DisplayStuffEmpty); |
71 | 72 |
|
72 | 73 | coolCustomUnityEvent.AddListener(DisplayStuffCustomParameters);
|
73 | 74 |
|
74 |
| -MyCoolCustomEvent += DisplayStuffEmpty; |
| 75 | +myCoolCustomEvent += DisplayStuffEmpty; |
75 | 76 |
|
76 | 77 | myEventHandler += DisplayStuff;
|
77 | 78 | }
|
@@ -83,20 +84,20 @@ void Update()
|
83 | 84 | if (Input.GetKeyDown(KeyCode.Space))
|
84 | 85 | {
|
85 | 86 | //Built-in
|
86 |
| -MyCoolEvent?.Invoke(this, null); |
| 87 | +myCoolEvent?.Invoke(this, null); |
87 | 88 |
|
88 |
| -MyCoolEventWithParameters?.Invoke(this, new MyName("InsertFunnyName")); |
| 89 | +myCoolEventWithParameters?.Invoke(this, new MyName("InsertFunnyName")); |
89 | 90 |
|
90 |
| -MyCoolEventAction?.Invoke(new MyName("InsertFunnyName"), new MyAge(5)); |
| 91 | +myCoolEventAction?.Invoke(new MyName("InsertFunnyName"), new MyAge(5)); |
91 | 92 |
|
92 |
| -CoolUnityEvent?.Invoke(); |
| 93 | +coolUnityEvent?.Invoke(); |
93 | 94 |
|
94 | 95 | coolCustomUnityEvent?.Invoke(new MyName("InsertFunnyName"), new MyAge(5));
|
95 | 96 |
|
96 | 97 | //Custom
|
97 | 98 | myEventHandler?.Invoke(this, null);
|
98 | 99 |
|
99 |
| -MyCoolCustomEvent?.Invoke(); |
| 100 | +myCoolCustomEvent?.Invoke(); |
100 | 101 | }
|
101 | 102 | }
|
102 | 103 |
|
|
0 commit comments