ツールチップイベントは、ポインターの下にあるビジュアル要素がツールチップを表示できるかどうかを確認するために送信されます。これはエディター専用のイベントです。
ツールチップは通常、tooltip プロパティを使用して設定されます。また、ツールチップイベントに応答してツールチップを設定することもできます。
ツールチップイベントは、2 つの方法で処理できます。
TooltipEvent へのコールバックを設定します。これによって、ツールチップが設定されていないビジュアル要素にツールチップが追加されます。ビジュアル要素に設定されているツールチップをオーバーライドすることもできます。ExecuteDefaultAction メソッドをオーバーライドします。コールバックを設定する、またはカスタムのビジュアル要素を実装してツールチップを宣言する場合は、コードや UXML で tooltip プロパティの値を設定しないでください。
tooltip プロパティを設定すると、マウスカーソル下のビジュアル要素は、TooltipEvent を処理するコールバックを自動的に登録します。また、このコールバックは、イベントのそれ以上の伝搬を停止します。
カスタムコールバックを登録して TooltipEvent を処理する場合は、イベントの伝搬を停止しなければなりません。そうしないと、後の伝播段階でツールチップがオーバーライドされてしまう可能性があります。
ツールチップイベントの基本クラスは EventBase クラスです。
| イベント | 説明 | 下降 | 上昇 | キャンセル可能 |
|---|---|---|---|---|
| TooltipEvent | Unity がツールチップを表示する直前に送信されます。 | はい | はい | はい |
rect: パネル座標系で浮遊する矩形のビジュアル要素。
tooltip: tooltip プロパティは、tooltip イベント中にツールチップボックス内に表示するテキスト文字列です。以下のコールバックイベントでは、イベント中に tooltip プロパティを設定します。
evt.tooltip = "Tooltip set by parent!";
TooltipEvent は、Unity エディターにツールチップが表示される直前に送信されます。ハンドラーは 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";
// 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();
}
}
}