Pointer events
Events that occur when the user interacts with a pointer device.
読み終わるまでの所要時間 7 分最終更新 14日前
Pointer events fire for UI interactions with a pointing device. Similar to mouse events, pointer events provide additional information about the used input device, such as pen pressure or tilt angle.
Pointer events always precede mouse events in UI Toolkit.
Pointer events don't have a persistent position. They also don't have a set position when they're released from the touch device.
Some pointer events, such as and events, have conditions triggered by the operating system (OS) of the input device.
PointerStationaryEventPointerCancelEventThe base class for all pointer events is PointerEventBase.
The following table describes the pointer events and their propagation phases:
Event | Description | Trickles down | Bubbles up | Cancellable |
|---|---|---|---|---|
| PointerDownEvent | Sent when you press a pointer. | Yes | Yes | Yes |
| PointerUpEvent | Sent when you release a pointer. | Yes | Yes | Yes |
| PointerMoveEvent | Sent when the pointer changes state. | Yes | Yes | Yes |
| PointerEnterEvent | Sent when the pointer enters a visual element, or one of its descendants. | Yes | No | Yes |
| PointerLeaveEvent | Sent when the pointer leaves a visual element and all of its descendants. | Yes | No | Yes |
| PointerOverEvent | Sent when the pointer enters a visual element. | Yes | Yes | Yes |
| PointerOutEvent | Sent when the pointer leaves a visual element. | Yes | Yes | Yes |
| PointerStationaryEvent | Sent when a pointer type (like a stylus or finger) doesn't change for a set amount of time determined by the operating system. | Yes | Yes | Yes |
| PointerCancelEvent | Sent when a pointer action is cancelled by the operating system. | Yes | Yes | Yes |
Unique properties
altitudeAngleazimuthAnglebuttonInteger | Button |
|---|---|
| 0 | Left button |
| 1 | Right button |
| 2 | Middle button |
clickCountdeltaPositiondeltaTimelocalPositionmodifiersShiftCtrlAltpointerIdpointerTypepositionpressedButtonsThe number is the sum of the individual buttons’ integer value (refer to the table below). For example, holding the right mouse button and the middle mouse button pressed at the same time will result in having a value of 6.
pressedButtonsInteger | Button |
|---|---|
| 1 | Left button |
| 2 | Right button |
| 4 | Middle button |
pressureradiusradiusVarianceradiusVarianceradiustangentialPressuretwistEvent list
The following list provides the name, description, and target of each event in the event family. For more information on the event, refer to the UI Toolkit API.
PointerDownEvent
The PointerDownEvent is sent when you press a pointer inside a visual element.
targetPointerUpEvent
A PointerUpEvent triggers when you release a pointer within a visual element.
When the event triggers, it also removes the pointer coordinates. It also clears the cache of the pointer, so there's no record of the pointer location.
PointerUpEventtargetPointerMoveEvent
The PointerMoveEvent occurs when the pointer changes state.
targetPointerEnterEvent
The PointerEnterEvent is sent when the pointer enters a visual element, or one of its descendants.
targetPointerLeaveEvent
A PointerLeaveEvent is sent when the pointer leaves a visual element and all its descendants. For example if a visual element contains a child, then the parent element receives this event when the pointer is no longer over either the parent or the child. The parent element doesn't receive the PointerLeaveEvent while the pointer is still over one of its child elements, even if it's no longer the topmost element underneath the pointer. It receives the PointerOverEvent instead.
targetPointerOverEvent
The PointerOverEvent is sent when the pointer enters a visual element.
targetPointerOutEvent
The PointerOutEvent is sent when the pointer leaves a visual element.
targetPointerStationaryEvent
The PointerStationaryEvent is sent when a pointer type (like a stylus or finger) doesn't change for a set amount of time determined by the operating system.
targetPointerCancelEvent
The PointerCancelEvent is sent when a pointer action is cancelled by the operating system.
targetExamples
The following code sample creates an Editor window with a red box containing a yellow box. It prints messages to the console when the pointer leaves a visual element or its child. It demonstrates the behavior of the PointerOutEvent and the PointerLeaveEvent.
To see the example in action, do the following:
- Under Assets > Scripts > Editor, create a new UXML file called PointerEventsTestWindow.uxml
- Copy the UXML code from below into it
- Under Assets > Scripts > Editor, create a new C# file called PointerEventsTestWindow.cs
- Copy a code sample into the C# script.
- From the Editor Toolbar, select Window > UI Toolkit > Pointer Events Test Window.
UXML code
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False"> <ui:VisualElement style="flex-grow: 1; justify-content: center; align-items: center;"> <ui:VisualElement name="Red_Box" style="background-color: rgb(183, 34, 46); width: 50%; height: 50%; align-items: center; justify-content: center;"> <ui:VisualElement name="Yellow_Box" style="width: 175%; height: 50%; background-color: rgb(197, 163, 0);" /> </ui:VisualElement> </ui:VisualElement></ui:UXML>
C# code
using UnityEditor;using UnityEngine;using UnityEngine.UIElements;using UnityEditor.UIElements;public class PointerEventsTestWindow : EditorWindow{ [MenuItem("Window/UI Toolkit/Pointer Events Test Window")] public static void ShowExample() { PointerEventsTestWindow wnd = GetWindow<PointerEventsTestWindow>(); wnd.titleContent = new GUIContent("Pointer Events Test Window"); } public void CreateGUI() { // Import UXML VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/Editor/PointerEventsTestWindow.uxml"); visualTree.CloneTree(rootVisualElement); // Get the red box and register pointer event callbacks VisualElement redBox = rootVisualElement.Q("Red_Box"); redBox.RegisterCallback<PointerOutEvent>(OnPointerOutEvent, TrickleDown.TrickleDown); redBox.RegisterCallback<PointerLeaveEvent>(OnPointerLeaveEvent, TrickleDown.TrickleDown); } private void OnPointerLeaveEvent(PointerLeaveEvent evt) { Debug.Log($"Pointer LEAVE Event. Target: {(evt.target as VisualElement).name}"); } private void OnPointerOutEvent(PointerOutEvent evt) { Debug.Log($"Pointer OUT Event. Target: {(evt.target as VisualElement).name}"); }}