Version: 2022.3
언어: 한국어
포인터 이벤트
전환 이벤트

툴팁 이벤트

툴팁 이벤트는 포인터 아래의 시각적 요소가 툴팁을 표시할 수 있는지 확인하기 위해 전송됩니다. 이 이벤트는 에디터 전용입니다.

툴팁은 일반적으로 tooltip 프로퍼티를 사용하여 설정됩니다. 툴팁 이벤트에 응답하여 툴팁을 설정할 수도 있습니다.

툴팁 이벤트는 다음의 두 가지 방식으로 처리할 수 있습니다.

  1. 콜백을 TooltipEvent로 설정합니다. 그러면 설정된 툴팁이 없는 시각적 요소에 툴팁이 추가됩니다. 시각적 요소에 설정된 툴팁을 오버라이드할 수도 있습니다.
  2. 커스텀 VisualElement를 선언(예: VisualElement를 확장하는 클래스 선언)하고 ExecuteDefaultAction 메서드를 오버라이드합니다.

콜백을 설정하거나 커스텀 시각적 요소를 구현하여 툴팁을 선언하는 경우 코드나 UXML을 통해 tooltip 프로퍼티의 값을 설정하지 마십시오.

tooltip 프로퍼티를 설정하면 마우스 커서 아래의 시각적 요소가 TooltipEvent를 처리하기 위한 콜백을 자동으로 등록합니다. 또한 이 콜백은 이벤트의 추가 전파를 중단시킵니다.

커스텀 콜백을 등록하여 TooltipEvent를 처리하려는 경우 이벤트 전파를 중단해야 합니다. 그러지 않으면 전파 단계에서 나중에 툴팁이 오버라이드될 수 있습니다.

툴팁 이벤트의 기본 클래스는 EventBase 클래스입니다.

이벤트 설명 트리클다운 버블업 취소 가능
TooltipEvent Unity가 툴팁을 표시하기 직전에 전송됩니다. 지원 지원 지원

고유한 프로퍼티

rect: 패널 좌표 시스템에서 커서가 놓여진 시각적 요소의 직사각형입니다.

tooltip: tooltip 프로퍼티는 tooltip 이벤트 동안 툴팁 상자 안에 표시할 텍스트 문자열입니다. 다음 콜백 이벤트는 이벤트 동안 tooltip 프로퍼티를 설정합니다.

   evt.tooltip = "Tooltip set by parent!";

이벤트 리스트

TooltipEvent

TooltipEvent는 Unity 에디터가 툴팁을 표시하기 직전에 전송됩니다. 핸들러는 TooltipEvent.tooltip 문자열과 TooltipEvent.rect를 설정해야 합니다.

target: 마우스 아래에 있는 시각적 요소입니다.

예제

다음 예제는 ToolTipEvent의 동작을 보여줍니다.

예제를 보려면 다음 단계를 따르십시오.

  1. Assets > Scripts > Editor에서 SampleWindow라는 C# 스크립트를 작성합니다.
  2. 다음 예제 중 하나를 복사하여 C# 스크립트에 붙여넣습니다.
  3. 에디터 툴바에서 Window > UI Toolkit > SampleWindow를 선택합니다.

예제 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";

       // If you comment out the registration of the callback, the tooltip that displays for the label is "And this is a tooltip".
       // If you keep the registration of the callback, the tooltip that displays for the label (and any other child of rootVisualElement)
       // is "Tooltip set by parent!".
       rootVisualElement.RegisterCallback<TooltipEvent>(evt =>
       {
           evt.tooltip = "Tooltip set by parent!";
           evt.rect = (evt.target as VisualElement).worldBound;
           evt.StopPropagation();
       }, TrickleDown.TrickleDown); // Pass the TrickleDown.TrickleDown parameter to intercept the event before it reaches the label.
   }
}

예제 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)
   {
       // Other events need to be handled as usual.
       base.ExecuteDefaultAction(evt);

       if (evt.eventTypeId == TooltipEvent.TypeId())
       {
           TooltipEvent e = (TooltipEvent)evt;

           // Apply an offset to the tooltip position.
           var tooltipRect = new Rect(worldBound);
           tooltipRect.x += 10;
           tooltipRect.y += 10;
           e.rect = tooltipRect;

           // Set a custom/dynamic tooltip. 
           e.tooltip = $"This is instance # {m_CurrentCounter + 1} of my CustomLabel";

           // Stop propagation avoids other instances of handling of the event that may override the values set here.
           e.StopPropagation();
       }
   }
}

포인터 이벤트
전환 이벤트