Game Development Reference
In-Depth Information
Now, create a new scene and add an empty GameObject into the scene, then rename
it to
SimpleEventObject
(as this GameObject is just there to attach the script to,
it doesn't matter what it is actually called). Then either drag the script to the new
SimpleEventObject
GameObject or click on
Add
Component
in the inspector
(with the new
SimpleEventObject
GameObject selected) and find the
SimpleEvent
script to add to it.
Once you have added the script to the
SimpleEventObject
, then you see the
following view in the Inspector:
Inspector showing the UnityEvent property drawer
You should recognize the property drawer shown as it's the same one that's used by
the built-in UI components that can be interacted with, such as the Button, Slider,
and so on. It works the same way too because they also use
UnityEvents
.
Note that it doesn't have to be a UI object you attach the script to; it
doesn't even need to have an
EventSystem
in the scene for
UnityEvents
to function!
With the
SimpleEvent
script attached to the
SimpleEventObject
GameObject,
when you run your project now, the script will:
1. Call the
Start
method when the scene runs.
2. Register the
MyAwesomeDelegate
function as a delegate of the
new
UnityEvent.
3.
Invoke
(run) the
UnityEvent
, which causes all delegates to fire.
4.
Run the
MyAwesomeDelegate
method and output to the debug console.
This is all nice and neat but pretty much the same as what you can do with regular
delegates. However, as with the other
UnityEvent
implementations, we can also
configure the
UnityEvent
in the inspector thanks to its very useful
Property drawer
.