캡처 이벤트는 마우스 캡처 상태 변경을 알립니다. UI 툴킷에는 두 가지 타입의 캡처 이벤트가 있습니다.
요소가 마우스나 포인터를 캡처하는 경우, 해당 요소는 기기가 캡처를 릴리스하거나 잃을 때까지 포인팅 기기에서 이벤트를 받는 유일한 요소입니다.
예를 들어 텍스트 상자를 클릭하면 텍스트 상자가 마우스를 캡처합니다. 이때 마우스는 계속해서 화면에서 움직일 수 있지만, 텍스트 상자 바깥에서 이벤트를 트리거하지는 않습니다. 텍스트 박스가 마우스를 캡처하는 동안에는 다른 이벤트가 트리거되지 않습니다. 사용자가 텍스트 상자 외부에서 마우스 버튼을 누르면 상자가 마우스 캡처를 릴리스합니다.
이벤트 | 설명 | 트리클다운 | 버블업 | 취소 가능 |
---|---|---|---|---|
MouseCaptureEvent | 요소가 마우스 캡처를 받을 때 전송됩니다. | ✔ | ✔ | |
MouseCaptureOutEvent | 요소가 마우스 캡처를 릴리스하거나 다른 방식으로 잃을 때 전송됩니다. | ✔ | ✔ | |
PointerCaptureEvent | 요소가 포인터를 캡처할 때 전송됩니다. | ✔ | ✔ | |
PointerCaptureOutEvent | 요소가 포인터를 릴리스할 때 전송됩니다. | ✔ | ✔ |
마우스 캡처 이벤트는 물리적 마우스의 이벤트 또는 물리적 마우스를 에뮬레이트하는 가상 마우스를 의미합니다. 마우스를 캡처하면 마우스 포인터에 PointerCaptureEvent
가 발생합니다.
요소가 마우스 캡처를 릴리스하면 상응하는 MouseCaptureOutEvent
가 트리거됩니다. 타겟은 캡처 릴리스를 요청한 요소입니다.
동시에 두 개의 요소가 마우스를 캡처할 수 없습니다. 다른 시각적 요소가 MouseCaptureEvent
를 트리거하면 원래 MouseCaptureEvent
를 보낸 요소가 캡처를 잃습니다. 또한 이 경우 원본 요소에 MouseCaptureOutEvent
가 트리거됩니다.
UI 툴킷에서 포인터 이벤트는 마우스 이벤트에 선행합니다. 포인터 타입이 마우스인 경우, 포인터를 캡처하면 상응하는 마우스 캡처 이벤트도 트리거됩니다. 포인터를 캡처하면 마우스도 캡처됩니다.
MouseCaptureEvent
이벤트는 요소가 마우스 캡처를 받을 때 전송됩니다.
target
: 캡처를 받는 요소입니다.
MouseCaptureOutEvent
이벤트는 요소가 마우스 캡처를 릴리스하거나 잃을 때 전송됩니다.
target
: 캡처를 잃는 요소입니다.
PointerCaptureEvent
이벤트는 요소가 포인터 캡처를 받을 때 전송됩니다.
target
: 캡처를 받는 요소입니다.
PointerCaptureOutEvent
이벤트는 요소가 포인터 캡처를 릴리스하거나 잃을 때 전송됩니다.
target
: 캡처를 잃는 요소입니다.
다음 예제는 캡처 이벤트의 동작과 포인터 캡처 및 릴리스를 나타냅니다.
예제의 동작을 확인하려면 다음 단계를 따르십시오.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class CaptureEventsTestWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/Capture Events Test Window")]
public static void ShowExample()
{
var wnd = GetWindow<CaptureEventsTestWindow>();
wnd.titleContent = new GUIContent("Capture Events Test Window");
}
private bool m_IsCapturing = false;
public void CreateGUI()
{
// Add a few clickable labels that print a message to the console when clicked
for (int i = 0; i < 4; i++)
{
Label clickableLabel = new Label($"Label {i} - Click Me!");
clickableLabel.RegisterCallback<MouseDownEvent>((evt) => { Debug.Log($"Clicked on label '{(evt.target as Label).text}'"); });
rootVisualElement.Add(clickableLabel);
}
// Now add a label that captures the pointer
Label capturingLabel = new Label("Click here to capture mouse");
capturingLabel.RegisterCallback<MouseDownEvent>((evt) =>
{
if (!m_IsCapturing)
{
capturingLabel.text = "Click here to release mouse";
MouseCaptureController.CaptureMouse(capturingLabel);
m_IsCapturing = true;
}
else
{
capturingLabel.text = "Click here to capture mouse";
MouseCaptureController.ReleaseMouse(capturingLabel);
m_IsCapturing = false;
}
});
rootVisualElement.Add(capturingLabel);
// Register callbacks to print a message when the mouse is captured or released
rootVisualElement.RegisterCallback<MouseCaptureEvent>((evt) =>
{
Debug.Log("Mouse captured");
});
rootVisualElement.RegisterCallback<MouseCaptureOutEvent>((evt) =>
{
Debug.Log("Mouse captured released");
});
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.