Introduction and use of PointerEventData of Unity UGUI

Introduction and use of PointerEventData of Unity UGUI

1. What is PointerEventData?

PointerEventData is an important component in the UGUI system in Unity, which is used to handle pointer events input by users. It can obtain the user's click, drag, scroll and other operations, and provides a series of properties and functions to handle these events.

2. How PointerEventData works

PointerEventData converts user input events into events in Unity by encapsulating the underlying input system. It can obtain information such as the user's click position, click type, click object, etc., and pass this information to the corresponding event processing function.

3. Common properties of PointerEventData

  • position: Get the screen coordinates of the user's click.
  • delta: Get the displacement amount dragged by the user.
  • button: Get the mouse button clicked by the user.
  • clickCount: Get the number of user clicks.
  • pointerEnter: Get the UI object where the mouse pointer is located.

4. Common functions of PointerEventData

  • GetPress(): Determine whether the mouse button is pressed.
  • GetPressDown(): Determine whether the mouse button has just been pressed.
  • GetPressUp(): Determine whether the mouse button has just been lifted.
  • IsPointerMoving(): Determine whether the mouse pointer is moving.
  • IsPointerOverGameObject(): Determine whether the mouse pointer is on the UI object.

5. Complete sample code

Example 1: Get the mouse click position

using UnityEngine;
using UnityEngine.EventSystems;

public class ClickPosition : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("点击位置:" + eventData.position);
    }
}

Steps:

  1. Create an empty object and attach the script to it.
  2. Click the mouse in the scene to see the console output where you clicked.

Precautions:

  • The script needs to be mounted on the object with the Collider component in order to receive mouse click events.

Example 2: Determine if the mouse button is pressed

using UnityEngine;
using UnityEngine.EventSystems;

public class ButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("鼠标按钮按下");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("鼠标按钮抬起");
    }
}

Steps:

  1. Create a button and attach the script to it.
  2. Press and lift the mouse button to see the console output.

Precautions:

  • The script needs to be mounted on the Button component in order to receive mouse button events.

Example 3: Obtain mouse drag displacement

using UnityEngine;
using UnityEngine.EventSystems;

public class DragPosition : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("拖拽位移:" + eventData.delta);
    }
}

Steps:

  1. Create an object and attach the script to it.
  2. Hold down the left mouse button and drag the object, and check the drag displacement output by the console.

Precautions:

  • The script needs to be mounted on the object with the Collider component in order to receive mouse drag events.

Example 4: Determine whether the mouse pointer is on the UI object

using UnityEngine;
using UnityEngine.EventSystems;

public class PointerOverUI : MonoBehaviour
{
    public void Update()
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("鼠标指针在UI对象上");
        }
    }
}

Steps:

  1. Create a UI object and attach the script to an empty object.
  2. Move the mouse pointer over the UI object to see the console output.

Precautions:

  • It is necessary to mount the script on an empty object, and determine whether the mouse pointer is on the UI object in the Update function.

Example 5: Get the number of mouse clicks

using UnityEngine;
using UnityEngine.EventSystems;

public class ClickCount : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("点击次数:" + eventData.clickCount);
    }
}

Steps:

  1. Create a button and attach the script to it.
  2. Click the button continuously and see the console output of the number of clicks.

Precautions:

  • The script needs to be mounted on the Button component in order to receive mouse click events.

References

Guess you like

Origin blog.csdn.net/alianhome/article/details/131927798
Recommended