@@ -15,19 +15,19 @@ public class DifferentEventAlternatives : MonoBehaviour
15
15
16
16
//C# built-in EventHandler
17
17
//Requires "using System;"
18
- public event EventHandler myCoolEvent ;
18
+ public event EventHandler MyCoolEvent ;
19
19
//With parameters
20
- public event EventHandler < MyName > myCoolEventWithParameters ;
20
+ public event EventHandler < MyName > MyCoolEventWithParameters ;
21
21
22
22
23
23
//C# built-in Action
24
24
//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 ;
25
+ public event Action < MyName , MyAge > MyCoolEventAction ;
26
26
27
27
28
28
//Unity built-in UnityEvent
29
29
//Requires that we are "using UnityEngine.Events;"
30
- public UnityEvent coolUnityEvent = new UnityEvent ( ) ;
30
+ public UnityEvent CoolUnityEvent = new UnityEvent ( ) ;
31
31
//If you have parameters you have to create a new event class that inherits from UnityEvent<parameter1, parameter2, ...>
32
32
public MyCustomUnityEvent coolCustomUnityEvent = new MyCustomUnityEvent ( ) ;
33
33
//There's also something called UnityAction
@@ -49,6 +49,9 @@ public class DifferentEventAlternatives : MonoBehaviour
49
49
//Custom delegate with the same parameters as built-in EventHandler
50
50
public delegate void MyEventHandler ( object sender , EventArgs e ) ;
51
51
//Custom delegate with no parameters
52
+
53
+ public MyEventHandler myEventHandler ;
54
+
52
55
public delegate void MyEventHandlerEmpty ( ) ;
53
56
54
57
//The event belonging to the custom delegate
@@ -58,17 +61,19 @@ public class DifferentEventAlternatives : MonoBehaviour
58
61
59
62
void Start ( )
60
63
{
61
- // MyCoolEvent += DisplayStuff;
64
+ MyCoolEvent += DisplayStuff ;
62
65
63
- // MyCoolEventWithParameters += DisplayStuffCustom ;
66
+ MyCoolEventWithParameters += DisplayStuffCustomArgs ;
64
67
65
- // MyCoolEventAction += DisplayStuffCustomBig ;
68
+ MyCoolEventAction += DisplayStuffCustomParameters ;
66
69
67
- // CoolUnityEvent.AddListener(DisplayStuffEmpty);
70
+ CoolUnityEvent . AddListener ( DisplayStuffEmpty ) ;
68
71
69
72
coolCustomUnityEvent . AddListener ( DisplayStuffCustomParameters ) ;
70
73
71
- //MyCoolCustomEvent += DisplayStuffEmpty;
74
+ MyCoolCustomEvent += DisplayStuffEmpty ;
75
+
76
+ myEventHandler += DisplayStuff ;
72
77
}
73
78
74
79
@@ -78,20 +83,20 @@ void Update()
78
83
if ( Input . GetKeyDown ( KeyCode . Space ) )
79
84
{
80
85
//Built-in
81
- //myCoolEvent ?.Invoke(this, null);
86
+ MyCoolEvent ? . Invoke ( this , null ) ;
82
87
83
- // MyCoolEventWithParameters?.Invoke(this, new MyName("InsertFunnyName"));
88
+ MyCoolEventWithParameters ? . Invoke ( this , new MyName ( "InsertFunnyName" ) ) ;
84
89
85
- // MyCoolEventAction?.Invoke(new MyName("InsertFunnyName"), new MyAge(5));
90
+ MyCoolEventAction ? . Invoke ( new MyName ( "InsertFunnyName" ) , new MyAge ( 5 ) ) ;
86
91
87
- // CoolUnityEvent?.Invoke();
92
+ CoolUnityEvent ? . Invoke ( ) ;
88
93
89
94
coolCustomUnityEvent ? . Invoke ( new MyName ( "InsertFunnyName" ) , new MyAge ( 5 ) ) ;
90
95
91
96
//Custom
92
- //MyCoolCustomEvent ?.Invoke(this, null);
97
+ myEventHandler ? . Invoke ( this , null ) ;
93
98
94
- // MyCoolCustomEvent?.Invoke();
99
+ MyCoolCustomEvent ? . Invoke ( ) ;
95
100
}
96
101
}
97
102
0 commit comments