腳本語言:C#編程
附上一張圖說明Unity GUI編程中可用的控件:(可能有遺漏)ide
下面列出一些例子來講明:函數
一、Groups :ui
在固定Layout模式中起到組織可用項的功能,它讓你在屏幕的一個區域中包含多個控件。把定義的控件放在GUI.BeginGroup()和 GUI.EndGroup()這對函數中間,全部控件的位置座標都以Groups的0座標爲起點,假如更改了group座標,那麼內部的控件也會跟隨改變。this
示例代碼:spa
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 = 120; int groundHeight = 150; // group組初始位置 int groupx = (screenWidth - groundWidth) / 2; int groupy = (screenHeight - groundHeight) / 2; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight)); GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(10,30,100,30),"Level 1")) Debug.Log("Level 1"); if(GUI.Button( new Rect(10,70,100,30),"Level 2")) Debug.Log("Level 2"); if(GUI.Button(new Rect(10,110,100,30),"Level 3")) Debug.Log("Level 3"); GUI.EndGroup(); // 改變group座標,group組的位置隨之改變 groupx = (screenWidth - groundWidth) / 4; groupy = (screenHeight - groundHeight) / 4; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight)); GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(10,30,100,30),"Level 1")) Debug.Log("Level 1"); if(GUI.Button( new Rect(10,70,100,30),"Level 2")) Debug.Log("Level 2"); if(GUI.Button(new Rect(10,110,100,30),"Level 3")) Debug.Log("Level 3"); GUI.EndGroup(); } }
二、Button:3d
用來繪製響應單擊事件的按鈕;code
(1)普通按鈕示例代碼:orm
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 % 2) < 1) { if( GUI.Button( new Rect(10,10,120,100),"Unity Button")) print("用戶單擊了按鈕"); } } }
按鈕會閃爍顯示;blog
(2)帶圖標按鈕:
對應Main Camera:
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(20,20,200,120), new GUIContent(icon) ) ) print("用戶單擊了按鈕"); } }
三、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 (10, 10, Screen.width * 0.5f, Screen.height * 0.5f), "This is a title"); GUI.Box (new Rect (150, 170, texture.width/4, texture.height/4), texture); } }
四、Window:
可拖動的窗口;
示例代碼:
using UnityEngine; using System.Collections; public class GUITest5 : MonoBehaviour { public Rect windowRect0 = new Rect(20,20,150,0); // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI() { //渲染窗口ID爲0 windowRect0 = GUILayout.Window(0,windowRect0, DoMyWindow,"Draggable Window"); } void DoMyWindow(int windowID) { GUILayout.Label("This is a draggable window!"); } }
五、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 = 0; private bool submitted = false; private Rect windowRect0; void Start(){ } void Update(){ } void OnGUI() { var screenWidth = Screen.width; var screenHeight = Screen.height; var windowWidth = 300; var windowHeight = 180; var windowX = (screenWidth - windowWidth) / 2; var windowY = (screenHeight - windowHeight) / 2; //將窗口放置到屏幕中間 windowRect0 = new Rect(windowX,windowY,windowWidth,windowHeight); GUILayout.Window(0,windowRect0,UserForm,"User information"); } void UserForm(int windowID) { GUILayout.BeginVertical(); //first name GUILayout.BeginHorizontal(); GUILayout.Label("First Name",GUILayout.Width(80)); firstName = GUILayout.TextField(firstName); GUILayout.EndHorizontal(); //last name GUILayout.BeginHorizontal(); GUILayout.Label("Last Name",GUILayout.Width(80)); lastName = GUILayout.TextField(lastName); GUILayout.EndHorizontal(); //Age GUILayout.BeginHorizontal(); GUILayout.Label("Age",GUILayout.Width(80)); string ageText = GUILayout.TextField(age.ToString()); uint newAge = 0; 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 = 0; submitted = false; } if(submitted) { GUILayout.Label("submitted!"); } GUILayout.EndVertical(); } }
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 = 380; int groupHeight = 110; int screenWidth = Screen.width; int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / 2; int groupY = (screenHeight - groupHeight) / 2; GUI.BeginGroup( new Rect(groupX,groupY,groupWidth,groupHeight)); GUI.Box( new Rect(0,0,groupWidth,groupHeight),"Audio Settings"); GUI.Label( new Rect(10,30,100,30),"Master Volume"); masterVolume = GUI.HorizontalSlider( new Rect(120,35,200,30), masterVolume,0.0f,1.0f); GUI.Label(new Rect(330,30,50,30),"(" + masterVolume.ToString("f2") + ")"); GUI.Label(new Rect(10,70,100,30),"Effect Volume"); sfxVolume = GUI.HorizontalSlider(new Rect(120,75,200,30),sfxVolume,0.0f,1.0f); GUI.Label(new Rect(330,70,50,30),"(" + sfxVolume.ToString("f2") + ")"); GUI.EndGroup(); } }
七、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[10]; void OnGUI() { int groupWidth = 320; int groupHeight = 260; int screenWidth = Screen.width; int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / 2; int groupY = (screenHeight - groupHeight) / 2; GUI.BeginGroup(new Rect(groupX,groupY,groupWidth,groupHeight)); GUI.Box(new Rect(0,0,groupWidth,groupHeight),"Equalizer"); for(int i = 0; i < equalizerValues.Length; i++) { equalizerValues[i] = GUI.VerticalSlider(new Rect(i * 30 + 20,30,20,200),equalizerValues[i],0.0f,1.0f); } GUI.EndGroup(); } }
推薦Unity下的2D界面用NGUI來作,更方便。