鼠标指针当前悬停在其上或具有键盘焦点的控件的工具提示。(只读)
创建 GUI 控件时,您可以为其传入工具提示。为此,请将内容参数更改为接受自定义的 GUIContent 对象,
而不是仅传入要显示的字符串。
当鼠标悬停在带有工具提示的控件上时,它会将全局 GUI.tooltip 值设置为您传入的工具提示。
如果鼠标未悬停在任何控件上,则该值将设置为具有键盘焦点的控件。
在 OnGUI 代码的末尾,您可以创建一个显示 GUI.tooltip 值的标签。\
当鼠标悬停在按钮上时,将在 Game 视图上显示 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"); } }
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.