Game Development Reference
In-Depth Information
When the project is run this time, it will give us the same effect as with the code
declaration. However, it is now being performed using the editor configuration,
a world of choice at your fingertips. (You should see two output events, one from
code and one from the editor configured option.)
Well, that is all well and good being able to click on something and start an action,
but what about if we also want to send some data?
Using a parameter
We can expand on the previous example by using a set of parameters as part
of the
UnityEvent
call. First you need to declare a new class type using the
parameter pattern you wish to use. This needs to derive from (or be based upon)
the
UnityEvent
class using the data type of the information you want to pass. For
example, let's create a new
C#
script called
MyParameterizedEventClass
that will
derive from
UnityEvent
using a string parameter as follows:
using System;
using UnityEngine.Events;
[Serializable]
public class MyParameterizedEventClass : UnityEvent<string>{}
If you don't add the
[Serializable]
attribute tag to the class type,
the
UnityEvent
will not be available in the editor inspector!
(Which you may want to enforce.)
Also, don't forget to add the following
using
statement to the script
file (for the
Serializable
attribute), else you will get errors:
using System;
With our new type, we can now create and use the event as before but now with an
optional parameter. So create a new
C#
script called
SimpleParameterizedEvent
,
and replace its contents with the following code:
using System;
using UnityEngine;
using UnityEngine.Events;
public class SimpleParameterizedEvent : MonoBehaviour {
//My parameterized UnityEvent Manager
public MyParameterizedEventClass myParameterizedEvent =
new MyParameterizedEventClass();