Unity GUI编程

时间:2021-07-13 20:13:54

  脚本语言:C#

附上一张图说明Unity GUI编程中可用的控件:(可能有遗漏)

Unity GUI编程

下面列出一些例子来说明:

1、Groups :

  在固定Layout模式中起到组织可用项的功能,它让你在屏幕的一个区域中包含多个控件。把定义的控件放在GUI.BeginGroup()和 GUI.EndGroup()这对函数中间,所有控件的位置坐标都以Groups的0坐标为起点,假如更改了group坐标,那么内部的控件也会跟随改变。

示例代码:

using UnityEngine;
using System.Collections; public class Test3 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
// 屏幕宽度和高度
int screenWidth = Screen.width;
int screenHeight = Screen.height; // group组大小
int groundWidth = ;
int groundHeight = ; // group组初始位置
int groupx = (screenWidth - groundWidth) / ;
int groupy = (screenHeight - groundHeight) / ; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select");
if(GUI.Button( new Rect(,,,),"Level 1"))
Debug.Log("Level 1");
if(GUI.Button( new Rect(,,,),"Level 2"))
Debug.Log("Level 2");
if(GUI.Button(new Rect(,,,),"Level 3"))
Debug.Log("Level 3");
GUI.EndGroup(); // 改变group坐标,group组的位置随之改变
groupx = (screenWidth - groundWidth) / ;
groupy = (screenHeight - groundHeight) / ; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(,,,),"Level 1"))
Debug.Log("Level 1");
if(GUI.Button( new Rect(,,,),"Level 2"))
Debug.Log("Level 2");
if(GUI.Button(new Rect(,,,),"Level 3"))
Debug.Log("Level 3");
GUI.EndGroup();
}
}

Group

2、Button:

  用来绘制响应单击事件的按钮;

(1)普通按钮示例代码:

using UnityEngine;
using System.Collections; public class GUITest1 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
if ((Time.time % ) < ) {
if( GUI.Button( new Rect(,,,),"Unity Button"))
print("用户单击了按钮");
}
}
}

Button1

按钮会闪烁显示;

(2)带图标按钮:

对应Main Camera:

Unity GUI编程

Icon是Test2脚本中定义的public Texture 变量,直接把图片拉至Icon处即可产生对应关系。

示例代码:

using UnityEngine;
using System.Collections; public class Test2 : MonoBehaviour { public Texture icon; // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
if( GUI.Button( new Rect(,,,), new GUIContent(icon) ) )
print("用户单击了按钮");
}
}

Button2

3、Box:

  Box控件用来绘制带有边框背景的文字或图片。

示例代码:

using UnityEngine;
using System.Collections; public class GUITest4 : MonoBehaviour { public Texture texture; // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI()
{
// 指定为灰色颜色
GUI.color = Color.gray;
GUI.Box (new Rect (, , Screen.width * 0.5f, Screen.height * 0.5f), "This is a title");
GUI.Box (new Rect (, , texture.width/, texture.height/), texture);
}
}

Box

4、Window:

  可拖动的窗口;

示例代码:

using UnityEngine;
using System.Collections; public class GUITest5 : MonoBehaviour { public Rect windowRect0 = new Rect(,,,); // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
}
void OnGUI()
{
//渲染窗口ID为0
windowRect0 = GUILayout.Window(,windowRect0, DoMyWindow,"Draggable Window");
} void DoMyWindow(int windowID)
{
GUILayout.Label("This is a draggable window!");
}
}

window

5、GUILayout.beginHorizontal和GUILayout.beginVertical

  默认情况下,当使用GUILayout函数时所有的视图中的组件都会竖直排列。可以使用GUILayout.BeginHorizontal和GUILayout.EndHorizontall静态函数使控件相邻排放.每出现一次GUILayout.BeginVertical必须有对应的GUILayout.EndVertical与其对应,每出现一次GUILayout.BeginHorizontal则必须有对应的GUILayout.EndHorizontal与其对应;

示例代码:

using UnityEngine;
using System.Collections; public class GUITest6 : MonoBehaviour {
private string firstName = "First Name";
private string lastName = "Last Name";
private uint age = ;
private bool submitted = false; private Rect windowRect0; void Start(){
} void Update(){
} void OnGUI()
{
var screenWidth = Screen.width;
var screenHeight = Screen.height; var windowWidth = ;
var windowHeight = ;
var windowX = (screenWidth - windowWidth) / ;
var windowY = (screenHeight - windowHeight) / ; //将窗口放置到屏幕中间
windowRect0 = new Rect(windowX,windowY,windowWidth,windowHeight); GUILayout.Window(,windowRect0,UserForm,"User information");
} void UserForm(int windowID)
{
GUILayout.BeginVertical(); //first name
GUILayout.BeginHorizontal();
GUILayout.Label("First Name",GUILayout.Width());
firstName = GUILayout.TextField(firstName);
GUILayout.EndHorizontal(); //last name
GUILayout.BeginHorizontal();
GUILayout.Label("Last Name",GUILayout.Width());
lastName = GUILayout.TextField(lastName);
GUILayout.EndHorizontal(); //Age
GUILayout.BeginHorizontal();
GUILayout.Label("Age",GUILayout.Width());
string ageText = GUILayout.TextField(age.ToString());
uint newAge = ;
if( uint.TryParse(ageText, out newAge) )
{
age = newAge;
}
GUILayout.EndHorizontal(); if(GUILayout.Button("Submit"))
{
submitted = true;
}
if(GUILayout.Button("Reset"))
{
firstName = "First Name";
lastName = "Last Name";
age = ;
submitted = false;
}
if(submitted)
{
GUILayout.Label("submitted!");
}
GUILayout.EndVertical();
}
}
 
6、HorizontalSlider:
  水平滚动条
示例代码:
using UnityEngine;
using System.Collections; public class GUITest7 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} private float masterVolume = 1.0f;
private float sfxVolume = 1.0f; void OnGUI()
{
int groupWidth = ;
int groupHeight = ; int screenWidth = Screen.width;
int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / ;
int groupY = (screenHeight - groupHeight) / ; GUI.BeginGroup( new Rect(groupX,groupY,groupWidth,groupHeight));
GUI.Box( new Rect(,,groupWidth,groupHeight),"Audio Settings"); GUI.Label( new Rect(,,,),"Master Volume");
masterVolume = GUI.HorizontalSlider( new Rect(,,,), masterVolume,0.0f,1.0f);
GUI.Label(new Rect(,,,),"(" + masterVolume.ToString("f2") + ")"); GUI.Label(new Rect(,,,),"Effect Volume");
sfxVolume = GUI.HorizontalSlider(new Rect(,,,),sfxVolume,0.0f,1.0f);
GUI.Label(new Rect(,,,),"(" + sfxVolume.ToString("f2") + ")"); GUI.EndGroup();
}
}

HorizontalSlider

7、VerticalSlider:

  竖直滚动条

示例代码:

using UnityEngine;
using System.Collections; public class GUITest8 : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } private float[] equalizerValues = new float[]; void OnGUI()
{
int groupWidth = ;
int groupHeight = ; int screenWidth = Screen.width;
int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / ;
int groupY = (screenHeight - groupHeight) / ; GUI.BeginGroup(new Rect(groupX,groupY,groupWidth,groupHeight));
GUI.Box(new Rect(,,groupWidth,groupHeight),"Equalizer"); for(int i = ; i < equalizerValues.Length; i++)
{
equalizerValues[i] = GUI.VerticalSlider(new Rect(i * + ,,,),equalizerValues[i],0.0f,1.0f);
}
GUI.EndGroup();
}
}

VerticalSlider

  

  推荐Unity下的2D界面用NGUI来做,更方便。