Unity 툴킷 - 툴팁 이벤트 가이드
개요
Unity 사용자 인터페이스(UI) 생성시 툴팁 이벤트를 활용하는 방법에 대해 설명합니다. 툴팁 이벤트는 UI 요소에 툴팁을 표시하기 위해 사용됩니다. 이 문서에서는 툴팁 이벤트의 처리 방법과 예제를 제공합니다.
툴팁 이벤트 기본 사항
툴팁 이벤트는 포인터 아래 시각적 요소가 툴팁을 표시할 수 있도록 확인하기 위해 전송됩니다. 다음은 툴팁 이벤트와 관련된 주요 사항입니다:
- 툴팁은 일반적으로
tooltip
프로퍼티를 사용하여 설정합니다. - 툴팁 이벤트를 처리하는 방법은 두 가지입니다:
- 콜백 등록:
TooltipEvent
를 설정하여 툴팁을 지정합니다. - 커스텀 VisualElement 선언:
ExecuteDefaultAction
메서드를 오버라이드하여 재정의합니다.
툴팁 이벤트 처리
이벤트 종류 | 설명 | 사용자에 대한 지원 | 고유한 프로퍼티 |
---|---|---|---|
TooltipEvent | Unity가 툴팁을 표시하기 직전에 전송됩니다. | 지원 | rect, tooltip |
- rect: 패널 좌표 시스템에서 커서가 놓여진 시각적 요소의 직사각형입니다.
- tooltip: 툴팁 이벤트 동안 표시할 텍스트 문자열. 예)
evt.tooltip = "Tooltip set by parent!";
예제
예제 1: 부모 시각적 요소의 TooltipEvent에 대한 콜백 등록
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class SampleWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/SampleWindow")]
public static void ShowExample()
{
SampleWindow wnd = GetWindow<SampleWindow>();
wnd.titleContent = new GUIContent("SampleWindow");
}
public void CreateGUI()
{
VisualElement label = new Label("Hello World! This is a UI Toolkit Label.");
rootVisualElement.Add(label);
label.tooltip = "And this is a tooltip";
rootVisualElement.RegisterCallback<TooltipEvent>(evt =>
{
evt.tooltip = "Tooltip set by parent!";
evt.rect = (evt.target as VisualElement).worldBound;
evt.StopPropagation();
}, TrickleDown.TrickleDown);
}
}
예제 2: 커스텀 시각적 요소 선언과 ExecuteDefaultAction 오버라이드
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class SampleWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/SampleWindow")]
public static void ShowExample()
{
SampleWindow wnd = GetWindow<SampleWindow>();
wnd.titleContent = new GUIContent("SampleWindow");
}
private void CreateGUI()
{
CustomLabel custom1 = new CustomLabel("custom 1");
rootVisualElement.Add(custom1);
CustomLabel custom2 = new CustomLabel("custom 2");
rootVisualElement.Add(custom2);
}
}
public class CustomLabel : Label
{
private static int m_InstanceCounter = 0;
private int m_CurrentCounter;
public CustomLabel(string labelText) : base(labelText)
{
m_CurrentCounter = m_InstanceCounter++;
}
protected override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt.eventTypeId == TooltipEvent.TypeId())
{
TooltipEvent e = (TooltipEvent)evt;
var tooltipRect = new Rect(worldBound)
{
x = worldBound.x + 10,
y = worldBound.y + 10
};
e.rect = tooltipRect;
e.tooltip = $"This is instance # {m_CurrentCounter + 1} of my CustomLabel";
e.StopPropagation();
}
}
}
요약
이 문서에서 툴팁 이벤트와 그 처리를 위한 다양한 방법에 대해 알아보았습니다. 제공된 예제를 통해 툴팁을 설정하고 커스터마이즈하는 방법을 연습해보세요. Unity의 UI ToolKit을 활용하여 더욱 인터랙티브한 사용자 경험을 제공할 수 있습니다.