- Events are way of saying something happened without knowing or caring about who is listing.
- You have publisher and Subscriber.
Publisher has its events.
Subscriber can subscribe to that events. Multiple subscribers can subscribed to the same events.
- When something happens the publisher fires off the events and all these subscriber get modified that the event was fired.
- Events are specialized delegates that are useful delegate that are useful for when you want to alert other class that something has happened.
- Events function are very similar to public multicast delegate.
- An Event can be thought of like a broadcast system, any class that is interested in an event can subscriber methods to it.
Example 1 : EventHandler
- EventHandler which is inside Using.System.
- EventArgs is the standard way for passing more information through the events.
- This is just the normal .net standard delegate.
Example 2 : Delegate
Example 3 : Action
- We can also use the default delegate called Action.
- Action is the delegate the return void.
Example 4 : UnityEvent
- UnityEvent is inside the UnityEngine.Events
- In the Inspector you will have a field for UnityEvent.
- Main benefit of UnityEvent are that you can easily set them in the editor.