包含此页的版本:
不含此页的版本:
本节将解释使用 Unity 的即时模式 GUI 系统 (IMGUI) 编写控件脚本的基本必要条件。
Unity 的 IMGUI 控件使用了一个名为 OnGUI() 的特殊函数。只要启用包含的脚本,每帧都会调用 OnGUI() 函数 - 就像 Update() 函数一样。
IMGUI 控件本身的结构非常简单。这种结构在下面的示例中很明显。
/* Example level loader */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
void OnGUI ()
{
// Make a background box
GUI.Box(new Rect(10,10,100,90), "Loader Menu");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if(GUI.Button(new Rect(20,40,80,20), "Level 1"))
{
Application.LoadLevel(1);
}
// Make the second button.
if(GUI.Button(new Rect(20,70,80,20), "Level 2"))
{
Application.LoadLevel(2);
}
}
}
此示例是一个完整的、功能齐全的级别加载器。如果您复制/粘贴此脚本并将其附加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表,当您进入播放模式时,您会看到以下菜单出现:
让我们来看看示例代码的详细信息:
第一条 GUI 线,GUI。框(矩形 (10,10,100,90),“加载器菜单”);显示一个带有标题文本“加载器菜单”的 Box 控件。它遵循典型的 GUI 控制声明方案,我们将稍后探讨。
下一个 GUI 行是按钮控件声明。请注意,它与 Box Control 声明略有不同。具体来说,整个 Button 声明都放在 if 语句中。当游戏运行并单击 Button 时,此 if 语句返回 true,并执行 if 块内的任何代码。
由于每帧都会调用 OnGUI() 代码,因此无需显式创建或销毁 GUI 控件。声明控件的行与创建它的行相同。如果需要在特定时间显示控件,可以使用任何类型的脚本逻辑来执行此作。
/* Flashing button example */
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 是控件类型,通过调用 Unity 的 GUI 类或 GUILayout 类中的函数来声明,本指南的布局模式部分对此进行了详细讨论。例如,GUI。Label() 将创建一个非交互式标签。所有不同的控件类型将在后面指南的控件部分进行解释。
位置是任何 GUI 控制函数中的第一个参数。参数本身由 Rect() 函数提供。Rect() 定义了四个属性:最左边的位置、最顶部的位置、总宽度、总高度。所有这些值都以整数形式提供,对应于像素计算机图像中的最小单位。像素大小取决于您的屏幕分辨率。像素光照是在每个屏幕像素下计算的。更多信息
请参阅术语表值。所有 UnityGUI 控件都在屏幕空间中工作,屏幕空间是已发布播放器的分辨率(以像素为单位)。
坐标系以左上角为基础。Rect(10, 20, 300, 100) 定义了一个 Rectangle,该矩形从坐标 10,20 开始,到坐标 310,120 结束。值得重复的是,Rect() 中的第二对值是总宽度和高度,而不是控件结束的坐标。这就是为什么上面提到的例子以 310,120 而不是 300,100 结束的原因。
您可以使用 Screen.width 和 Screen.height 属性来获取播放器中可用屏幕空间的总尺寸。以下示例可能有助于阐明如何完成此作:
/* Screen.width & Screen.height example */
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");
}
}
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 公共变量,并将变量名称作为内容参数传递,如下所示:
/* Texture2D Content example */
public Texture2D controlTexture;
...
void OnGUI ()
{
GUI.Label (new Rect (0,0,100,50), controlTexture);
}
下面是一个更接近真实场景的示例:
/* Button Content examples */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
public Texture2D icon;
void OnGUI ()
{
if (GUI.Button (new Rect (10,10, 100, 50), icon))
{
print ("you clicked the icon");
}
if (GUI.Button (new Rect (10,70, 100, 20), "This is text"))
{
print ("you clicked the text button");
}
}
}
还有第三个选项,允许您在 GUI 控件中同时显示图像和文本。您可以提供 GUIContent 对象作为 Content 参数,并定义要在 GUIContent 中显示的字符串和图像。
/* Using GUIContent to display an image and a string */
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 中定义工具提示,并在鼠标悬停在 GUI 中其他位置时将其显示在 GUI 中的其他位置。
/* Using GUIContent to display a tooltip */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
// This line feeds "This is the tooltip" into GUI.tooltip
GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", "This is the tooltip"));
// This line reads and displays the contents of GUI.tooltip
GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
}
}
还可以使用 GUIContent 显示字符串、图标和工具提示。
/* Using GUIContent to display an image, a string, and a tooltip */
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);
}
}
GUIContent 构造函数的脚本参考页面有一些其使用示例。