Game Development Reference
In-Depth Information
Creating clickable text elements
To process whether any of the preceding elements have been clicked or not by the
mouse pointer, we have to manually program the handling of this event. Luckily for us,
since these components are attached to a GameObject (which inherits from
MonoBe-
havior
), we can use mouse events that
MonoBehavior
provides.
Detecting mouse clicks
Whenever the mouse pointer is clicked while the pointer is over the top of a GameOb-
ject, the
OnMouseDown
callback is invoked. With this, we can trap these button clicks
and respond accordingly:
void OnMouseDown() { // insert code here }
Detecting mouse over
A second callback method is called whenever the mouse pointer moves onto a
GameObject. This function is a convenient way to handle the highlighting of the GUI
elements when they are selected or browsed:
void OnMouseOver() { // insert code here }
Detecting leaving mouse over
A final callback method is called whenever the mouse leaves from over the top of a
GameObject. This is the complement of the preceding
mouseOver
handler and can
be used in conjunction with the preceding method of turning highlighting on and off:
void OnMouseExit() { // insert code here }