Game Development Reference
In-Depth Information
To keep this example simple, we will put all the
UnityEvent
code in a
single code file called
AlarmSystem
. This is optional, of course, and you
can create separate scripts for each section if you so wish.
Having the code all together like this will just make it easier to maintain
all the crisscrossed event code.
So create a new
C#
script called
AlarmSystem
and replace its contents with the
following (just the base using statements we need):
using UnityEngine;
using UnityEngine.EventSystems;
For our alarm, we only need the location of the alarm trigger point to send our drones
in which would look like the following code, add this to our AlarmSystem script:
// Custom data we will send via the event system
public class AlarmEventData : BaseEventData
{
public Vector3 AlarmTriggerData;
public AlarmEventData(EventSystem eventSystem,
Vector3 alarmTriggerData): base(eventSystem)
{
AlarmTriggerData = alarmTriggerData;
}
}
The important parts of this class definition are:
• The class derives from the
BaseEventData
class
• The class constructor passes the
EventSystem
for the event back to the base
class constructor using the
base()
notation
• With our data in place we can now define our interface
I've used a
Vector3
here for the event data to identify the location of the
alarm that has been triggered. If you were just doing a 2D game then this
could be changed to a
Vector2
. Alternatively, you might want to add
more data such as the direction of the camera with another Vector. The
choice is up to you.