1、分组控件
原有的GUI系统可在屏幕上实现控件的分组行为,并体现了基于组件位置的多个相对位置
类似于ScrollView控件,各组件包含起始位置和结束位置,进而定义了组件内的全部控件的范围
void OnGUI()
{
//创建一个位置于(50,50),宽150,高60的组
GUI.BeginGroup(new Rect(50, 50, 150, 60));
GUI.Label(new Rect(10, 10, 100, 30), "Label in a Group");
GUI.EndGroup();
}
标签控件在组件界内予以hi之,位置为相对位置,即(50+10)=60处
2、命名控件
当各个控件通过脚本加以实现时,在设置时对其进行命名,对于控制流程、访问基于键盘操作的各个输入栏,或者根据当前所选焦点控件继承相关逻辑时,该命名操作十分必要
用户无法直接命名控件,仅表示为GUI系统命令,进而在屏幕上绘制内容,与渲染管线类似
对于Unity中GUI控件,可简单通知GUI系统,下一个即将绘制的控件包含的一个名称
string login = "Would you like to play a game?";
void OnGUI()
{
GUI.SetNextControlName("MyAwesomeField");
login=GUI.TextField(new Rect(10, 10, 200, 20), login);
}
3、获取焦点
当控件定义了对应的名称后,随后即可确定处于焦点状态的控件。
GUI.FocusControl("MyAwesomeField");
当控件处于焦点状态时,通过调用下列函数,可获取处于焦点状态的、特定控件的名称:
string selectedControl = GUI.GetNameOfFocusedControl();
作为命名和焦点机制的示例,用户可设置一个简单的登录GUI,并通过验证行为以及有效特性实现用户的输入操作
具体而言,可创建一个注册表
①定义基础类
using UnityEngine;
[ExecuteInEditMode]
public class IntermediateGUI : MonoBehaviour{
public string username="Enter username";
public string password="Enter password";
private bool passwordInError=false;
private string passwordErrorMessage="<Color=red>Password too short</color>";
}
②密码验证
void CheckUserPasswordAndRegister()
{
if(password.Length<6)
{
passwordInError=true;
GUI.FocusControl("PassWordField");
}else
{
passwordInError=false;
GUI.FocusControl("");
}
}
据此,可添加相应的GUI控件
void OnGUI()
{
GUI.BeginGroup(new Rect(Screen.width/2 - 75, Screen.height/2 - 80, 150, 160));
GUI.Box(new Rect(0, 0, 150, 160), "User registration from");
GUI.SetNextControlName("User registration form")
username=GUI.TextField(new Rect(10, 40, 130, 20), username);
GUI.SetNextControlName("PasswordField");
password=GUI.PasswordField(new Rect(10, 70, 130, 20 ), password, '*');
if(passwordInError)
{
GUI.Label(new Rect(10, 100, 200, 20), passwordErrorMessage);
}
if(Event.current.iskey && Event.current.keyCode==KeyCode.Return &&
GUI.GetNameOfFocusedControl()=="PasswordField")
{
CheckUserPasswordAndRegister();
}
if(GUI.Button(new Rect(80, 130, 65, 20),"Register"))
{
CheckUserPasswordAndRegister();
}
GUI.EndGroup();
}
4、工具提示
GUI控件通常会具有与其自身关联的工具提示功能,当空间处于焦点或鼠标指针悬停于其上时,将会显示某些额外的文本信息
当使用GUIContent类进行绘制时,替换该控件的内容即可,例如在前述脚本中更新Register按钮:
if(GUI.Button(new Rect(80, 130, 65, 20), new GUIContent("Register", "My Tooltip")))
{
CheckUserPasswordAndRegister();
}
当工具提示定义完毕后,可在屏幕某处显示其内容,且通常作为标签予以显示
可在按钮代码之后、EndGroup函数之前加入下列代码:
GUI.Label(new Rect (10, 120, 65, 20), GUI.tooltip);
5、Window控件
Window控件针对其他控件定义了独立的、可绘制的窗口,类似于ScrollView控件,但仅包含单层
当使用独立的Window控件时,用户可实现多项功能,包括:
①Window控件的模式特征,当前窗口为唯一可操控的窗口,非模式窗口则表示并列窗口
②Window控件的拖曳状态,可通过鼠标或触摸方式进行拖曳
③各个Window控件的绘制顺序可对彼此之上的绘制窗口进行排序
④如果村咋子多个并列窗口或模式窗口,则存在某一特定窗口处于焦点状态
当创建Window控件时,需要通过下列签名针对Window控件定义新的回调方法:
void DoMyWindow(int windowID)
{
GUI.Label(new Rect(25, 15, 100, 30),"Label");
GUI.DragWindow();
}
在Window控件方法的适当位置,仅需调用GUI.Window函数即可,根据相关属性,追踪Window控件位置
private Rect recWindow1;
void OnGUI()
{
Rect recWindow1;
recWindow1=GUI.Window(0, recWindow1, DoMyWindow, "Controls Window");
}
通过下列属性,可将Window控件调用至视图中:
#窗口ID
#Window控件开启后的Rect位置
#Window控件GUI内容的委托方法
#当前窗口的名称/标题
对应模式窗口,需要采用GUI.ModalWindow函数实例化窗口,而非Window函数
rctWindow1=GUI.ModalWindow(0, rctWindow1, DoMyWindow, "Modal Controls Window");