Introduction and use of the EventSystem (event system) component of Unity UGUI
1. What is the EventSystem component?
EventSystem is an important component in Unity UGUI, used to handle user input events, such as click, drag, scroll, etc. It is responsible for passing user input events to the appropriate UI elements and triggering corresponding event callback functions.
2. How the EventSystem component works
The EventSystem component uses ray detection to determine where a user input event occurs and delivers the event to the most appropriate UI element. It will determine the target object of the event based on the hierarchical relationship of UI elements and the results of ray detection.
3. Common properties of EventSystem components
firstSelectedGameObject
: Set the default selected UI element.sendNavigationEvents
: Whether to send navigation events.pixelDragThreshold
: The pixel threshold of the drag event.currentInputModule
: The currently used input module.
4. Commonly used functions of the EventSystem component
SetSelectedGameObject(GameObject selected)
: Set the currently selected UI element.RaycastAll(PointerEventData eventData, List<RaycastResult> resultAppendList)
: Execute ray detection and save the result to the specified list.UpdateModules()
: Update the input module.
5. Complete sample code
Example 1: Set the default selected button
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DefaultButton : MonoBehaviour
{
public Button defaultButton;
void Start()
{
EventSystem.current.SetSelectedGameObject(defaultButton.gameObject);
}
}
Steps:
- Create an empty object and attach the DefaultButton script to it.
- In the Inspector panel, assign the button that needs to be selected by default to the defaultButton variable.
Example 2: Click a button to trigger an event
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonClick : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Button clicked!");
}
}
Steps:
- Create a button and attach the ButtonClick script to it.
- Implement the function in the ButtonClick script
OnPointerClick
, and add the code that needs to be executed in the function.
Example 3: Dragging objects
using UnityEngine;
using UnityEngine.EventSystems;
public class DragObject : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
}
Steps:
- Create an object and attach the DragObject script to it.
- Implement the function in the DragObject script
OnDrag
, and modify the position of the object in the function.
Example 4: Scrolling List
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ScrollList : MonoBehaviour, IScrollHandler
{
public ScrollRect scrollRect;
public void OnScroll(PointerEventData eventData)
{
scrollRect.verticalNormalizedPosition += eventData.scrollDelta.y * 0.1f;
}
}
Steps:
- Create a scrolling list and attach the ScrollList script to it.
- Implement the function in the ScrollList script
OnScroll
, and modify the position of the scroll list in the function.
Example 5: Button Navigation
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Navigation : MonoBehaviour, ISelectHandler
{
public Button nextButton;
public void OnSelect(BaseEventData eventData)
{
EventSystem.current.SetSelectedGameObject(nextButton.gameObject);
}
}
Steps:
- Create multiple buttons and attach the Navigation script to them.
- Implement the function in the Navigation script
OnSelect
, and set the next selected button in the function.
Precautions
- There can only be one EventSystem component, and multiple EventSystems will cause input events to fail to process normally.
- The EventSystem component needs to be used in conjunction with other UI components, such as Button, ScrollRect, etc.
References
- Unity official documentation: EventSystem
- Unity official tutorial: UI Event System