The Tooltip event is sent to check if a visual elementA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary underneath the pointer is able to display a tooltip. This is an Editor-only event.
Tooltips are usually set using the tooltip
property. You can also respond to the Tooltip event to set tooltips.
You can handle the Tooltip event in two ways:
TooltipEvent
. 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.ExecuteDefaultAction
method.If you set the callback or implement a custom visual element to declare tooltips, don’t set the value for the tooltip
property via code or UXML.
When you set a tooltip
property, the visual element under the mouse cursor automatically registers a callback to handle the TooltipEvent
. This callback also stops further propagation of the event.
If you register a custom callback to handle the TooltipEvent
, you must stop the propagation of the event, or the tooltip can be overridden later in the propagation phase.
The base class for Tooltip events is the EventBase class.
Event | Description | Trickles down | Bubbles up | Cancellable |
---|---|---|---|---|
TooltipEvent | Sent just before Unity displays a tooltip. | Yes | Yes | Yes |
rect
: Rectangle of the hovered visual element in the panel coordinate system.
tooltip
: The tooltip
property is a text string to display inside the tooltip box during the tooltip
event. The following callback event sets the tooltip property during the event:
evt.tooltip = "Tooltip set by parent!";
The TooltipEvent
is sent just before the Unity Editor displays a tooltip. The handler should set the TooltipEvent.tooltip
string and the TooltipEvent.rect
.
target
: The visual element under the mouse.
The following examples display the behavior of the ToolTipEvent
.
To view an example:
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.
}
}
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();
}
}
}
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.