Introduction and use of PointerEventData of Unity UGUI
1. What is PointerEventData?
PointerEventData is an important component in the UGUI system in Unity, used to handle pointer events entered by users. It can obtain user clicks, drags, scrolls and other operations, and provides a series of attributes 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 user click location, click type, click object, etc., and pass this information to the corresponding event handling function.
3. Common properties of PointerEventData
-
position
: Get the screen coordinates that the user clicks. -
delta
: Get the displacement amount dragged by the user. -
button
: Get the mouse button clicked by the user. -
clickCount
: Get the number of clicks by the user. -
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 example code
Example 1: Get the mouse click position
using UnityEngine; using ; public class ClickPosition : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { ("Click location:" + ); } }
Operation steps:
- Create an empty object and mount the script to that object.
- Click the mouse in the scene to view the click location of the console output.
Notes:
- The script needs to be mounted on an object with the Collider component to receive mouse click events.
Example 2: Determine whether the mouse button is pressed
using UnityEngine; using ; public class ButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public void OnPointerDown(PointerEventData eventData) { ("Mouse button press"); } public void OnPointerUp(PointerEventData eventData) { ("Mouse button lift"); } }
Operation steps:
- Create a button and mount the script to that button.
- Press and lift the mouse button to view console output.
Notes:
- The script needs to be mounted on the Button component to receive mouse button events.
Example 3: Get the mouse drag displacement
using UnityEngine; using ; public class DragPosition : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { ("Drag displacement:" + ); } }
Operation steps:
- Create an object and mount the script to that object.
- Hold down the left mouse button to drag the object to view the drag displacement output by the console.
Notes:
- The script needs to be mounted on an object with the Collider component to receive mouse drag events.
Example 4: Determine whether the mouse pointer is on the UI object
using UnityEngine; using ; public class PointerOverUI : MonoBehaviour { public void Update() { if (()) { ("Mouse pointer on UI object"); } } }
Operation steps:
- Create a UI object and mount the script onto an empty object.
- Move the mouse pointer to the UI object and view the console output.
Notes:
- You need 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 ; public class ClickCount : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { ("Number of clicks:" + ); } }
Operation steps:
- Create a button and mount the script to that button.
- Click the button continuously to view the number of clicks output by the console.
Notes:
- The script needs to be mounted on the Button component to receive mouse click events.
References
Unity official documentation:
https://docs./ScriptReference/
Unity official tutorial:/tutorial/unity-ui
The above is the introduction and use of PointerEventData of Unity UGUI. For more information about the introduction and use of PointerEventData of Unity UGUI, please pay attention to my other related articles!