ClickEvent는 사용자가 VisualElement 위에 마우스 커서를 올리고 마우스 왼쪽 버튼(또는 포인팅 기기의 첫 번째 버튼)을 누르면 발생합니다.
클릭은 같은 VisualElement에서 포인터 다운 이벤트 발생 후 포인터 업 이벤트 발생으로 구성됩니다. 다운 이벤트와 업 이벤트가 같은 VisualElement에서 발생하는 한, 두 이벤트 사이에 포인터가 움직일 수 있습니다.
이 이벤트는 버튼이 아닌 시각적 요소에서 클릭을 감지하는 데 사용할 수 있습니다. 예를 들어, Toggle
컨트롤의 구현은 ClickEvent
를 사용하여 체크 표시를 나타내거나 숨기고 컨트롤의 값을 변경합니다.
ClickEvent
의 기본 클래스는 PointerEventBase입니다. 자세한 내용은 포인터 이벤트에 관한 문서를 참조하십시오.
이벤트 | 설명 | 트리클다운 | 버블업 | 취소 가능 |
---|---|---|---|---|
ClickEvent | 마우스 왼쪽 버튼을 클릭하면 발생합니다. | ✔ | ✔ | ✔ |
ClickEvent
는 고유한 프로퍼티가 없으나, 기본 클래스의 모든 프로퍼티를 상속합니다. 프로퍼티 리스트는 포인터 이벤트 페이지를 참조하십시오.
시각적 요소 위에 마우스 커서를 올리고 마우스 왼쪽 버튼을 클릭하면 Unity가 이 이벤트를 전송합니다.
target
: 클릭 발생 시 마우스나 포인팅 기기 아래에 있었던 요소입니다.
시각적 요소에서 ClickEvent
를 등록하려면 다음 코드를 사용하십시오.
visualElement.RegisterCallback<ClickEvent>(OnClickEvent);
다음 예제는 색칠된 시각적 요소에서 ClickEvent에 반응하는 방식을 나타냅니다. 요소를 클릭하면 요소의 컬러가 새로운 무작위 컬러로 변경됩니다.
예제의 동작을 확인하려면 다음을 수행하십시오.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class ClickEventExampleWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/ClickEventExample")]
public static void ShowExample()
{
var wnd = GetWindow<ClickEventExampleWindow>();
wnd.titleContent = new GUIContent("Click Event Example");
}
public void CreateGUI()
{
// Create a few different colored boxes
for (int i = 0; i < 4; i++)
{
// Create VisualElement with random background color
var newBox = new VisualElement() { style = { flexGrow = 1, backgroundColor = GetRandomColor() } };
rootVisualElement.Add(newBox);
// Register a click event to the visual element to change the background color to a new color
newBox.RegisterCallback<ClickEvent>(OnBoxClicked);
}
}
private void OnBoxClicked(ClickEvent evt)
{
// Only perform this action at the target, not in a parent
if (evt.propagationPhase != PropagationPhase.AtTarget)
return;
// Assign a random new color
var targetBox = evt.target as VisualElement;
targetBox.style.backgroundColor = GetRandomColor();
}
private Color GetRandomColor()
{
return new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f));
}
}
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.