GUI系统
在开发的过程中,我们经常会用到GUI(Graphical User Interface (图形页面系统))组件,项目中一般包含按钮、文本框、图片的插入及滑块等控件的应用,合理的设计和搭配可以搭建出优美的GUi,需要注意的是,GUi组件一般是通过坐标绘制的,,它是以左上角为坐标位置(0,0),右下角为坐标(Screen.width,Screen.height),并且是以像素为单位进行开发的。
GUI组件的变量
1.button
2.color
3.changed
4.tooltip
5.button
6.backgroundColor
7.contentColor
8.enable
9.depth
…
一:GUI.Button(new Rect(10, 100, Screen.height / 10, Screen.width / 10),“a button”)
在GUI.Button里面有两个参数,一个是按钮的大小,另一个是按钮中包含的文字
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class control : MonoBehaviour
{
public GUISkin[] gskin;
public int skin_Index = 0;
public Transform transform;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
skin_Index++;
if(skin_Index>=gskin.Length)
{
skin_Index = 0;
}
}
}
private void OnGUI()
{
GUI.skin = gskin[skin_Index];
if (GUI.Button(new Rect(10, 100, Screen.height / 10, Screen.width / 10),"a button"))
{
Debug.Log("a target");
}
GUI.Label(new Rect(0, Screen.height * 3 / 10, Screen.width / 10, Screen.height / 10), "a lable");
}
}
效果图如下
二:GUI.color = color.yellow;
注意:color变量是对全局GUI进行染色,背景和字体的颜色都会发生改变
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class control : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
GUI.color = Color.yellow;
GUI.Button(new Rect(100, 400, 100, 100), "sdasdad");
}
}
三:backgroundColor变量
它用于控制GUI组件的背景颜色
private void OnGUI()
{
GUI.backgroundColor = Color.blue;
GUI.Button(new Rect(100, 400, 100, 100), "sdasdad");
}
注意,在设置backgroundcolor的时候,系统只会将这之后创建的GUI组件的背景颜色修改为backgroundcolor,在设置前面创建的GUI组件不会被更改
四:centerColor变量
用于GUI组件中的文本着色,在开发过程中可以通过设置contentColor的值来改变GUI组件中文本的颜色,同样也只会将窗机之后的GUI组件的字体颜色改变,前面的不影响
private void OnGUI()
{
GUI.contentColor = Color.blue;
GUI.Button(new Rect(100, 400, 100, 100), "sdasdad");
}
五:changed变量
changed变量可以检测任何控件中输入数据的值是否发生改变,若改变则返回true,并根据要执行相应的操作输出一些提示信息
private void OnGUI()
{
stringToEdit = GUI.TextField(new Rect(100, 100, 150, 150),"文本信息");
if(GUI.changed)
{
Debug.Log("文本已经改变");
}
}
六:enabled变量
enable变量可以控制GUI组件是否被启用,在开发过程中可以对enabled的bool值进行设置,从而控制GUI组件的启用情况。
注意:enable控制的是在设置它的值后面的组件情况
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control2 : MonoBehaviour
{
public bool allOptions = true;
public bool extenedl1 = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
allOptions = GUI.Toggle(new Rect(0, 0, Screen.width / 5, Screen.height / 10), allOptions, "Edit All OPtions");
GUI.enabled = allOptions;
GUI.enabled = true;
extenedl1 = GUI.Toggle(new Rect(300, 300, Screen.width / 5, Screen.height / 10), extenedl1, "Extended Option1");
GUI.enabled = extenedl1;
GUI.enabled = true;
//GUI.enabled = true;
//在自定义的区域内回执一个名为ok的按钮没判断是否被按下
if (GUI.Button(new Rect(0, Screen.height * 3 / 10, Screen.width / 5, Screen.height / 10), "OK"))
{
print("user clicked ok");
}
}
}
在这段代码中,一共设置了两个Toggle开关,点击第一个开关,allOptions的值变成false,所以此时的GUI.enabled值变成了false,影响到了第二个Toggle开关没所以从新设为true,如果不这样重新复制的话,下面的Toggle组件会被设置成半透明,这种状态是不能点击改变的。
七:tooltip
tooltip是提示框变量,在创建GUI控件额时候,该变量可以传递一个工具作为提示消息,该变量可以通过改变内容参数去定义GUIContent物体
示例:
private void OnGUI()
{
GUI.Button(new Rect(Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10),new GUIContent("Click me", "This is the tooltip"));
GUI.Label(new Rect(Screen.width / 10, Screen.height / 5, Screen.width / 5, Screen.height / 10), GUI.tooltip);
}
当用户的鼠标悬停在Click me的按钮上时,提示消息,This is thetooltip,注意,这里面的GUI.tooltip需要用户提供,而代码中的GUIContent()定义如下
摘要:
// Build a GUIContent containing some text. When the user hovers the mouse over
// it, the global GUI.tooltip is set to the tooltip.
//
// 参数:
// text:
//
// tooltip:
所以GUIContent中的第二个参数提供了GUI.tooltip,由于只有鼠标悬停在上方的时候才会提供,所以也就有了我们需要的那个功能
同样,还可以为不同的按钮或者其他控件创造不同的提示消息,如下
private void OnGUI()
{
GUI.Box(new Rect(Screen.width / 20, Screen.height / 10, Screen.width * 3 / 5, Screen.height * 3 / 5), new GUIContent("Box", "this Box has a tooltip"));
GUI.Button(new Rect(Screen.width / 10, Screen.height / 3, Screen.width / 2, Screen.height / 10), "No tooltip");
GUI.Button(new Rect(Screen.width / 10, Screen.height / 2, Screen.width / 2, Screen.height / 10), new GUIContent("I have a tooltip", "The button overrider thr box"));
//在自定义的区域绘制一个标签,标签内容为GUI.tooltip提供的消息
GUI.Label(new Rect(Screen.width / 10, Screen.height / 5, Screen.width * 2 / 5, Screen.height / 10), GUI.tooltip);
}
八:depth变量
depth是按照控件的深度,对当前控件进行排序的行为,在多个脚本中,若有不同的脚本同时运行,则可以设置这个值来确定执行的顺序,一般的越大,越先执行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Text2 : MonoBehaviour
{
public static int guiDepth = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
GUI.depth = guiDepth;
if(GUI.RepeatButton(new Rect(Screen.width/5,Screen.height/5,Screen.width/5,Screen.height/5),"GoBack"))
{
guiDepth = 1;
Text1.guiDepth = 0;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Text1 : MonoBehaviour
{
// Start is called before the first frame update
public static int guiDepth = 0;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
GUI.depth = guiDepth;
if (GUI.RepeatButton(new Rect(Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 5), "Goback"))
{
guiDepth = 1;
Text2.guiDepth = 0;
}
}
}
效果如下: