Skip to content

Commit 626b03a

Browse files
3.Observer - DifferentEventAlternatives updated, was not functioning properly before, reference names were inconsistent.
1 parent 27f2016 commit 626b03a

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Assets/Patterns/3. Observer/Different events/DifferentEventAlternatives.cs

+20-15
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ public class DifferentEventAlternatives : MonoBehaviour
1515

1616
//C# built-in EventHandler
1717
//Requires "using System;"
18-
public event EventHandler myCoolEvent;
18+
public event EventHandler MyCoolEvent;
1919
//With parameters
20-
public event EventHandler<MyName> myCoolEventWithParameters;
20+
public event EventHandler<MyName> MyCoolEventWithParameters;
2121

2222

2323
//C# built-in Action
2424
//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;
2626

2727

2828
//Unity built-in UnityEvent
2929
//Requires that we are "using UnityEngine.Events;"
30-
public UnityEvent coolUnityEvent = new UnityEvent();
30+
public UnityEvent CoolUnityEvent = new UnityEvent();
3131
//If you have parameters you have to create a new event class that inherits from UnityEvent<parameter1, parameter2, ...>
3232
public MyCustomUnityEvent coolCustomUnityEvent = new MyCustomUnityEvent();
3333
//There's also something called UnityAction
@@ -49,6 +49,9 @@ public class DifferentEventAlternatives : MonoBehaviour
4949
//Custom delegate with the same parameters as built-in EventHandler
5050
public delegate void MyEventHandler(object sender, EventArgs e);
5151
//Custom delegate with no parameters
52+
53+
public MyEventHandler myEventHandler;
54+
5255
public delegate void MyEventHandlerEmpty();
5356

5457
//The event belonging to the custom delegate
@@ -58,17 +61,19 @@ public class DifferentEventAlternatives : MonoBehaviour
5861

5962
void Start()
6063
{
61-
//MyCoolEvent += DisplayStuff;
64+
MyCoolEvent += DisplayStuff;
6265

63-
//MyCoolEventWithParameters += DisplayStuffCustom;
66+
MyCoolEventWithParameters += DisplayStuffCustomArgs;
6467

65-
//MyCoolEventAction += DisplayStuffCustomBig;
68+
MyCoolEventAction += DisplayStuffCustomParameters;
6669

67-
//CoolUnityEvent.AddListener(DisplayStuffEmpty);
70+
CoolUnityEvent.AddListener(DisplayStuffEmpty);
6871

6972
coolCustomUnityEvent.AddListener(DisplayStuffCustomParameters);
7073

71-
//MyCoolCustomEvent += DisplayStuffEmpty;
74+
MyCoolCustomEvent += DisplayStuffEmpty;
75+
76+
myEventHandler += DisplayStuff;
7277
}
7378

7479

@@ -78,20 +83,20 @@ void Update()
7883
if (Input.GetKeyDown(KeyCode.Space))
7984
{
8085
//Built-in
81-
//myCoolEvent?.Invoke(this, null);
86+
MyCoolEvent?.Invoke(this, null);
8287

83-
//MyCoolEventWithParameters?.Invoke(this, new MyName("InsertFunnyName"));
88+
MyCoolEventWithParameters?.Invoke(this, new MyName("InsertFunnyName"));
8489

85-
//MyCoolEventAction?.Invoke(new MyName("InsertFunnyName"), new MyAge(5));
90+
MyCoolEventAction?.Invoke(new MyName("InsertFunnyName"), new MyAge(5));
8691

87-
//CoolUnityEvent?.Invoke();
92+
CoolUnityEvent?.Invoke();
8893

8994
coolCustomUnityEvent?.Invoke(new MyName("InsertFunnyName"), new MyAge(5));
9095

9196
//Custom
92-
//MyCoolCustomEvent?.Invoke(this, null);
97+
myEventHandler?.Invoke(this, null);
9398

94-
//MyCoolCustomEvent?.Invoke();
99+
MyCoolCustomEvent?.Invoke();
95100
}
96101
}
97102

0 commit comments

Comments
 (0)