鼠标当前悬停或键盘焦点所在的控件的工具提示。(只读)。
当你创建 GUI 控件时,你可以为它们传入一个工具提示。做法是将 content 参数更改为采用自定义 GUIContent 对象,而不仅仅是传入要显示的字符串。
当鼠标悬停在带有工具提示的控件上时,它会将全局 GUI.tooltip 的值设置为传入的工具提示。如果鼠标没有悬停在任何控件上,则值将设置为具有键盘焦点的控件。在 OnGUI 代码的末尾,你可以创建一个标签,显示 GUI.tooltip 的值。
游戏视图中的 GUI 工具提示会在鼠标悬停在按钮上时出现。
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void OnGUI() { // Make a button using a custom GUIContent parameter to pass in the tooltip. GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", "This is the tooltip"));
// Display the tooltip from the element that has mouseover or keyboard focus GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip); } }
你可以使用元素的排序来创建“分层”工具提示
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void OnGUI() { // This box is larger than many elements following it, and it has a tooltip. GUI.Box(new Rect(5, 35, 110, 75), new GUIContent("Box", "this box has a tooltip"));
// This button is inside the box, but has no tooltip so it does not // override the box's tooltip. GUI.Button(new Rect(10, 55, 100, 20), "No tooltip here");
// This button is inside the box, and HAS a tooltip so it overrides // the tooltip from the box. GUI.Button(new Rect(10, 80, 100, 20), new GUIContent("I have a tooltip", "The button overrides the box"));
// finally, display the tooltip from the element that has // mouseover or keyboard focus GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip); } }
工具提示还可以用于实现 OnMouseOver / OnMouseOut 消息传递系统
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public string lastTooltip = " ";
void OnGUI() { GUILayout.Button(new GUIContent("Play Game", "Button1")); GUILayout.Button(new GUIContent("Quit", "Button2"));
if (Event.current.type == EventType.Repaint && GUI.tooltip != lastTooltip) { if (lastTooltip != "") { SendMessage(lastTooltip + "OnMouseOut", SendMessageOptions.DontRequireReceiver); }
if (GUI.tooltip != "") { SendMessage(GUI.tooltip + "OnMouseOver", SendMessageOptions.DontRequireReceiver); }
lastTooltip = GUI.tooltip; } }
void Button1OnMouseOver() { Debug.Log("Play game got focus"); }
void Button2OnMouseOut() { Debug.Log("Quit lost focus"); } }