Game Development Reference
In-Depth Information
One really nice feature with the
Selectable
component is that it can be attached to any
GameObject on a UI Canvas. So if you want text that reacts to touch, the mouse, or
even an image (which is basically what the button is), then you can. You can even go
wild and make your own version of the
Selectable
control with your own features.
Remember, if you want to do anything with the Events that the
Selectable
component implements, you will need either an
Event
Trigger
component (covered in
Chapter 2
,
Building Layouts
) or a
script implementing the
Event
interfaces (like the button script),
which we will cover next.
An event occurred, what do I do?
Next we come to one of my favorite additions with the new UI system, graphical
event hookups.
The
Selectable
component listens, by default, to events, but the
Button
also needs
to act on those events to do something else, turn on a light, activate a Hero's skill or
even just exit the game (disabled by default of course). For this, the
Button
control
also implements the
IEventSystemHandler
and
IPointerClickHandler
interfaces.
These interfaces are just subcomponents of the
EventSystem
. The
EventSystem automatically searches for components and scripts that
implement these interfaces while it is running. The
Button
is similar
to the
Event Trigger
component we covered in the previous chapter;
however, it is designed to specifically handle only certain events and
avoids the overhead of providing all types of events (just the click
event, which is all the
Button
actually needs).
If we look at the
Event
portion of the
Button
control, we see the following: