One of the more complex but important features in Unity is the event system that dynamically enables interactions in a game. Be it a fast-paced action game or something that is widely used for mobile UI, one needs to grasp an understanding of Unity’s Event System to create responsive and immerse player experiences.
This article will discuss the basic building blocks of Unity’s Event System, how to step through establishing most basic interactions, and give examples of how to easily apply this to your projects to bring your game world to life.
Table of Contents
- Introduction to Unity’s Event System
- Key Components of the Event System
- Event System GameObject
- Event Trigger
- Event Listeners
- Setting Up a Simple UI Interaction
- Adding Interactions with Event Triggers
- Extending the Event System with Custom Events
- Practical Example: Creating a Button with Click Feedback
- Conclusion
Introduction to Unity’s Event System
The Unity Event System manages how user input such as clicks and touches relate to UI elements or game objects. It is a bridge between what occurs when the player is performing an action in the game, such as by pressing a button, and the action of the game, including perhaps its character moving or UI that is changing.
You don’t need to write too much complex code to take advantage of Unity’s Event System. Instead, you’re going to work with a combination of UI components, event listeners, and triggers to actually make interactions.
Core Components of the Event System
The Unity Event System consists of a few core components. Let’s break them down:
Event System GameObject
The Event System GameObject is actually the heart of Unity’s event system. This GameObject does the work of automatically detecting input, whether it is originating from a keyboard, mouse, or even a touch screen device. It often appears automatically when you add a UI element to your scene, but sometimes can be added manually if you think it is necessary.
Event Trigger
An Event Trigger allows you to assign various actions to UI elements or GameObjects. There is a set list of predefined events such as click, drag, pointer enters and exits, among others. This is where you can attach certain functions to the events providing instant feedback for the player.
Event Listeners
Event listeners watch for specific input events, such as clicking a button or working with objects. In Unity, components like buttons include built-in listeners that automatically respond to many of the common events, including a button click. You can also create your own listeners for GameObjects.
Simple UI Interaction Setup
Let’s create a very simple UI interaction with the Unity Event System. Here’s how to make a button, where clicking on it will send some event (like printing out a message or change the color of an object):
Add Button
- Select UI Button in the Hierarchy window by right-click. If there wasn’t an Event System in the scene, this will add a button GameObject and an Event System GameObject to your scene.
Customize the Button:
- You can now make changes to the button’s size, text and look by changing its Rect Transform and Text components in the Inspector.
Add a Script:
- To add functionality to this button, create a new C# script called ButtonClickHandler.cs and attach it to the button GameObject.
using UnityEngine;
using UnityEngine.UI;
public class ButtonClickHandler : MonoBehaviour
{
public void OnButtonClick()
{
Debug.Log("Button clicked!");
}
}Link the Button to the Script:
- In the Inspector window, find the Button component, and in the
OnClick()section, click the+symbol to add an event. - Drag the GameObject with the
ButtonClickHandlerscript into the empty slot, then choose theOnButtonClickmethod from the dropdown menu.
Now, when you click the button, “Button clicked!” will be printed in the Console.
Adding Interactions with Event Triggers
Even though a button click is an example of an input event, not all possible input events are as simple. Event Triggers allow more complex and varied input responses besides just hitting a button. For instance, you can use the PointerEnter event to determine when the mouse passes over a UI element, and Drag to track along with the cursor while dragging.
Let’s add an Event Trigger to the example.
Add an Event Trigger:
- Select your button in the Hierarchy and, in the Inspector, click
Add Component. Search forEvent Triggerand add it.
Event Trigger Setup:
- Add New Event Type and, within Event Types, select PointerEnter. This will be an event that triggers when the mouse pointer enters the area of the button.
Assign a Function to the Event:
- Just as we assigned a function to the button click event, now we assign a function to the PointerEnter event perhaps changing the color of the button.
using UnityEngine;
using UnityEngine.UI;
public class ButtonHoverHandler : MonoBehaviour
{
public Button button;
public void OnPointerEnter()
{
button.GetComponent<Image>().color = Color.red;
}
}When you hover over the button, the color will change to red.
5. Extending the Event System with Custom Events
The Event System can also be extended with custom events. Suppose you want to trigger a more complex set of actions like playing a sound, changing multiple UI elements, or triggering in-game events.
Here’s how you can set up a custom event system in Unity using UnityEvent:
using UnityEngine;
using UnityEngine.Events;
public class CustomEventTrigger : MonoBehaviour
{
public UnityEvent onCustomEvent;
void Start()
{
if (onCustomEvent == null)
onCustomEvent = new UnityEvent();
onCustomEvent.AddListener(OnEventTriggered);
}
public void TriggerEvent()
{
onCustomEvent.Invoke();
}
void OnEventTriggered()
{
Debug.Log("Custom event triggered!");
}
}Now, you can trigger this event from any action in your game and have multiple listeners respond to it.
Sample Exercise: Sound Feedback Button
For this final example, let’s make a complete interaction where clicking a button triggers a sound and changes the text of the button.
Add an Audio Source:
- Drag an audio clip into your scene and attach it to the button as an Audio Source component.
Modify the Button Script:
using UnityEngine;
using UnityEngine.UI;
public class ButtonFeedback : MonoBehaviour
{
public AudioSource clickSound;
public Text buttonText;
public void OnButtonClick()
{
clickSound.Play();
buttonText.text = "Clicked!";
}
}Attach in the Unity Editor the OnClick event of the button to play the sound and change the text. So, when the button is clicked now, it has a reaction and makes a sound.
Conclusion
Using Unity’s Event System, you just learned how to set up simple and complex interactions in your games. From simple button clicks to event-driven systems more comprehensive, the Unity Event System enables simple and scalable input handling and interaction. Using these tools, you will be creating more engaging experiences with your games or applications.
To the next steps: try to combine different input types and events like drag-and-drop systems or creating custom event listeners for unique in-game interactions.