Game Development Reference
In-Depth Information
With these events, it is possible to write a script to enable any GameObject
(even non-
Unity UI
related) to react to these events as they are generated from
the Unity
EventSystem
, for example:
using UnityEngine;
using UnityEngine.EventSystems;
public class eventHandler : MonoBehaviour, IPointerClickHandler {
#region IPointerClickHandler implementation
public void OnPointerClick (PointerEventData eventData)
{
//Do something with a click
}
#endregion
}
The
#region
syntax is extremely useful for creating readable
script files. It allows you to collapse or expand the code within a
#region
/
#endregion
block.
Check here for more details on using
#regions
to keep your code
This provides us with a base implementation to add click behavior to any
GameObject in a scene. More on this later
Remember, for the events to be managed and captured, you will need
the appropriate
RayCast
system attached to the camera that renders the
GameObject. For example, if you attached the previous script to a 3D
model, you would need to add the
Physics Raycaster
to the main 3D
camera and the 3D object would need some sort of 3D collider such as
Mesh collider
.
Event Triggers
Not to be outdone by the previous weighty code-based approach for using input
events from the
UnityEvent
system, there is also a just-as-powerful graphical
system to add events to GameObjects using
Event Triggers
.