ツールチップイベントは、ポインターの下にあるビジュアル要素がツールチップを表示できるかどうかを確認するために送信されます。これは、エディター専用のイベントです。
ツールチップは通常、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";
// コールバックの登録をコメントアウトすると、ラベルに表示されるツールチップは "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); // 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)
{
// 通常通り処理が必要な他のイベント
base.ExecuteDefaultAction(evt);
if (evt.eventTypeId == TooltipEvent.TypeId())
{
TooltipEvent e = (TooltipEvent)evt;
// ツールチップ位置にオフセットを適用
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";
// 伝播を停止して、他のインスタンスがここで設定した値をオーバーライドするイベントを処理することを防ぎます
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.