发送提示事件可以检查指针下方的视觉元素是否能够显示工具提示。该事件只能在编辑器中使用。
工具提示通常使用 tooltip
属性设置。通过响应工具提示事件,您还可以设置工具提示。
您可以通过两种方式处理工具提示事件:
TooltipEvent
的回调。这会向未设置的视觉元素添加工具提示。这也可以覆盖视觉元素已设置的工具提示。ExecuteDefaultAction
方法。如果您设置回调或实现自定义视觉元素来声明工具提示,请不要通过代码或 UXML 设置 tooltip
属性的值。
当您设置一个 tooltip
属性时,鼠标光标下的视觉元素会自动注册一个回调来处理 TooltipEvent
。此回调还会停止事件的进一步传播。
如果注册自定义回调来处理 TooltipEvent
,则必须停止事件的传播,否则工具提示可以稍后在传播阶段被覆盖。
工具提示事件的基类是 EventBase 类。
事件 | 描述 | 涓滴 | 冒泡 | 可取消 |
---|---|---|---|---|
TooltipEvent | 在 Unity 显示工具提示之前发送。 | ✔ | ✔ | ✔ |
rect
:面板坐标系中的悬停视觉元素的矩形。
tooltip
:tooltip
属性是在 tooltip
事件期间显示在工具提示框内的文本字符串。以下回调事件在事件期间设置工具提示属性:
evt.tooltip = "Tooltip set by parent!";
在 Unity 编辑器显示工具提示之前发送 TooltipEvent
。处理程序应该设置 TooltipEvent.tooltip
字符串和 TooltipEvent.rect
。
target
:鼠标下的视觉元素。
以下示例显示了“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";
// 如果注释掉回调的注册,则为标签显示的工具提示为 "And this is a tooltip"。
// 如果您保留回调的注册,则为标签(以及 rootVisualElement 的任何其他子项)显示的工具提示
// 为 "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); // 传递 TrickleDown.TrickleDown 参数以在事件到达标签之前拦截事件。
}
}
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;
// Apply an offset to the tooltip position.
var tooltipRect = new Rect(worldBound);
tooltipRect.x += 10;
tooltipRect.y += 10;
e.rect = tooltipRect;
// 设置自定义/动态工具提示。
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.