Tooltip event
Events that occur when the user interacts with a tooltip.
読み終わるまでの所要時間 3 分最終更新 15日前
The Tooltip event is sent to check if a visual element underneath the pointer is able to display a tooltip. This is an Editor-only event.
Tooltips are usually set using the property. You can also respond to the Tooltip event to set tooltips.
tooltipYou can handle the Tooltip event in two ways:
- Set a callback to the . This adds a tooltip to a visual element that doesn't have one set. This can also override the tooltip set to a visual element.
TooltipEvent - Declare a custom VisualElement (such as declaring a class that extends VisualElement), and override the method.
ExecuteDefaultAction
If you set the callback or implement a custom visual element to declare tooltips, don't set the value for the property via code or UXML.
tooltipWhen you set a property, the visual element under the mouse cursor automatically registers a callback to handle the . This callback also stops further propagation of the event.
tooltipTooltipEventIf you register a custom callback to handle the , you must stop the propagation of the event, or the tooltip can be overridden later in the propagation phase.
TooltipEventThe base class for Tooltip events is the EventBase class.
The following table describes the tooltip event and its propagation phases:
Event | Description | Trickles down | Bubbles up | Cancellable |
|---|---|---|---|---|
| TooltipEvent | Sent just before Unity displays a tooltip. | Yes | Yes | Yes |
Unique properties
recttooltiptooltiptooltipevt.tooltip = "Tooltip set by parent!";
Event list
TooltipEvent
The is sent just before the Unity Editor displays a tooltip. The handler should set the string and the .
TooltipEventTooltipEvent.tooltipTooltipEvent.recttargetExamples
The following examples display the behavior of the .
ToolTipEventTo view an example:
- Under Assets > Scripts > Editor, create a C# script called SampleWindow.
- Copy one of the following examples into the C# script.
- From the Editor Toolbar, select Window > UI Toolkit > SampleWindow.
Example 1: Registering a callback to the TooltipEvent on the parent visual element
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. }}
Example 2: Declaring a custom visual element and overriding 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(); } }}