开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第30天,点击查看活动详情
使用 IMGUI 创建控件
Unity 的 IMGUI 控件使用一个名为 OnGUI() 的特殊函数。只要启用包含脚本,就会在每帧调用 OnGUI()函数,就像 Update()函数一样。
/* 闪烁按钮示例 */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
if (Time.time % 2 < 1)
{
if (GUI.Button (new Rect (10,10,200,20), "Meet the flashing button"))
{
print ("You clicked me!");
}
}
}
}
此处,每隔一秒才调用一次 GUI.Button() ,因此按钮会出现再消失。当然,用户只能在按钮可见时单击按钮。
如您所见,可使用任何所需的逻辑来控制 GUI 控件的显示和运行时间。现在我们将探讨每个控件的声明的细节。
控件剖析
声明 GUI 控件时,需要三段关键信息:
Type(Position, Content)
可以看到,此结构是一个带有两个参数的函数。我们现在将探讨此结构的细节。
Type
Type是指 Control Type(控件类型);通过调用 Unity 的 [GUI 类]或 [GUILayout 类]中的函数来声明该类型(在本指南的[布局模式]部分对此进行了详细讨论)。例如,GUI.Label()将创建非交互式标签。本指南稍后的[控件]部分将介绍所有不同的控件类型。
Position
Position是所有GUI控件函数中的第一个参数。此参数本身随附一个Rect()函数。Rect()定义四个属性:最左侧位置 、 最顶部位置 、 总宽度、总高度 。所有这些值都以整数提供,对应于像素值。所有 UnityGUI 控件均在屏幕空间 (Screen Space)中工作,此空间表示已发布的播放器的分辨率(以像素为单位)。
坐标系基于左上角。Rect(10, 20, 300, 100) 定义一个从坐标 10,20 开始到坐标 310,120 结束的矩形。值得再次强调的是,Rect()中的第二对值是总宽度和高度,而不是控件结束的坐标。这就是为什么上面提到的例子结束于 310,120 而不是 300,100。
可使用Screen.width和Screen.height属性来获取播放器中可用的屏幕空间的总尺寸。以下示例可能有助于解释如何完成此操作:
/* Screen.width 和 Screen.height 示例 */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI()
{
GUI.Box (new Rect (0,0,100,50), "Top-left");
GUI.Box (new Rect (Screen.width - 100,0,100,50), "Top-right");
GUI.Box (new Rect (0,Screen.height - 50,100,50), "Bottom-left");
GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom-right");
}
}
Content
GUI 控件的第二个参数是要与控件一起显示的实际内容。通常会希望在控件上显示一些文本或图像。要显示文本,请将字符串作为 Content 参数传递,如下所示:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
GUI.Label (new Rect (0,0,100,50), "This is the text string for a Label Control");
}
}
要显示图像,请声明=Texture2D公共变量,并将变量名称作为 Content 参数传递,如下所示:
/* Texture2D 内容示例 */
public Texture2D controlTexture;
...
void OnGUI ()
{
GUI.Label (new Rect (0,0,100,50), controlTexture);
}
此外还可通过第三个选项在 GUI 控件中一起显示图像和文本。为此,可提供GUIContent对象作为 Content 参数,并定义要在 GUIContent 中显示的字符串和图像。
/* 使用 GUIContent 来显示图像和字符串 */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
public Texture2D icon;
void OnGUI ()
{
GUI.Box (new Rect (10,10,100,50), new GUIContent("This is text", icon));
}
}
此外,还可在 GUIContent 中定义__工具提示 (Tooltip),当鼠标悬停在 GUI 上时将工具提示显示在 GUI 中的其他位置。
/* 使用 GUIContent 来显示工具提示 */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
// 此行将 "This is the tooltip" 传入 GUI.tooltip
GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", "This is the tooltip"));
// 此行读取并显示 GUI.tooltip 的内容
GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
}
}
也可以使用 GUIContent 来显示字符串、图标和工具提示。
/* 使用 GUIContent 来显示图像、字符串和工具提示 */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
public Texture2D icon;
void OnGUI ()
{
GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", icon, "This is the tooltip"));
GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
}
}