GUI.tooltip 工具提示

static var tooltip : string

Description描述

The tooltip of the control the mouse is currently over, or which has keyboard focus. (Read Only).

控制鼠标当前通过对象的提升信息,或具有键盘焦点。

When you create GUI controls, you can pass in a tooltip for them. This is done by changing the content parameter to take a custom-made GUIContent object, rather than just passing in a string to display.

当你创建GUI控件的时候,你可以传递一个提示信息给他们。这可以通过改变内容参数去自定义GUIContent物体,而不是仅仅传递一个字符串。

When the mouse is over a control with a tooltip, it sets the global GUI.tooltip value to the tooltip you pass in. If the mouse is not howering over any control, the value is set to the control which has keyboard focus. At the end of the OnGUI code, you can make a label showing the value of GUI.tooltip

当鼠标通过控件有提示信息的控件时,它设置全局GUI.tooltip的值到你传递的提示信息。如果鼠标没有通过任何控件,这个值设置到有键盘焦点的控件。在OnGUI代码的最后你可以创建一个标签来显示GUI.tooltip的值。

GUI.tooltip 工具提示

GUI Tooltip on th Game view appears when the mouse is over the button.
当鼠标移到到按钮之上时,游戏视图显示的GUI工具提示。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void OnGUI() {
		GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", "This is the tooltip"));
		GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
	}
}
function OnGUI () {
	
	// Make a button using a custom GUIContent parameter to pass in the tooltip.
	//创建一个按钮使用自定义GUIContent参数去传递提升信息。
	GUI.Button(Rect(10,10,100,20), GUIContent ("Click me", "This is the tooltip"));

	// Display the tooltip from the element that has mouseover or keyboard focus
	//当鼠标或键盘焦点移到到控件上时,显示提示信息
	GUI.Label(Rect(10,40,100,40), GUI.tooltip);
}

You can use the ordering of elements to create 'hierarchical' tooltips:

你可以使用元素的次序来创建"层次"提示信息:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void OnGUI() {
		GUI.Box(new Rect(5, 35, 110, 75), new GUIContent("Box", "this box has a tooltip"));
		GUI.Button(new Rect(10, 55, 100, 20), "No tooltip here");
		GUI.Button(new Rect(10, 80, 100, 20), new GUIContent("I have a tooltip", "The button overrides the box"));
		GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
	}
}
function OnGUI () {
	//这个是最底层的Box,有提示
	GUI.Box(Rect(5, 35, 110, 75), GUIContent ("Box", "这是Box的提示信息"));

	//这个按钮在Box的里面,没有提示,它不会覆盖Box的提示
	GUI.Button(Rect(10, 55, 100, 20), "这个没有提示");

	//这个按钮也在Box的里面,有提示信息,它将覆盖Box的提示
	GUI.Button(Rect(10, 80, 100, 20),GUIContent ("我有提示", "我的提示会覆盖下面Box的提示"));
	// 在旁边的位置显示提示信息。
	GUI.Label(Rect(130,40,100,40), GUI.tooltip);
}

Tooltips can also be used to implement an OnMouseOver / OnMouseOut messaging system:

Tooltip也能用来实现 OnMouseOver / OnMouseOut 消息系统。

using UnityEngine;
using System.Collections;

public class example : 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");
	}
}
var lastTooltip : String = " ";

function OnGUI () {
	GUILayout.Button (GUIContent ("Play Game", "Button1"));
	GUILayout.Button (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;
	}
}

function Button1OnMouseOver () {
	Debug.Log("Play game got focus");
}

function Button2OnMouseOut () {
	Debug.Log("Quit lost focus");
}
最后修改:2011年6月15日 Wednesday 10:44

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。