UNITY3D GUI組件使用例子
2019/12/22??????點(diǎn)擊:
using UnityEngine; using System.Collections; public class Cube1Control : MonoBehaviour { public Texture texture; public Texture2D texture2D; public Texture2D texture2DActive; public string userName; public string password; public string remark; public bool isSuccess; public int select=0; public bool toggle1 = false; public Texture2D bug1; public Texture2D bug2; public float h; public Vector2 vector2; Rect rect1 = new Rect(0, 10, 300, 500); Rect rect2 = new Rect(600, 10, 300, 500); public int selGridId = 0; string[] selString = new string[] { "Grid1", "Grid2", "Grid3", "Grid4", "Grid5" }; // Use this for initialization void Start () { h = 40; } // Update is called once per frame void Update () { } void win(int id) { GUI.Button(new Rect(10, 120, 150, 50), "點(diǎn)擊按鈕"); //使用DragWindow啟用窗口拖動(dòng) GUI.DragWindow(); } void OnGUI() { #region GUILayout布局 ////GUILayout采用線性布局,類似于StackPanel,默認(rèn)是縱向布局。通過(guò)GUILayout.BeginHorizontal(); ////開(kāi)啟和GUILayout.EndHorizontal()結(jié)束一個(gè)橫向排列區(qū)域,同理BeginVertical() 、EndVertical()。 //GUILayout.BeginHorizontal(); //GUILayout.Button("Button1", GUILayout.Width(100), GUILayout.Height(50)); //GUILayout.Button("Button2", GUILayout.Width(100), GUILayout.Height(50)); //GUILayout.EndHorizontal(); //GUILayout.BeginVertical(); ////如果嫌控件太擠,可以使用GUILayout.Space(30);增加若干像素的間隙。 //GUILayout.Space(30);//Button3和Button1在垂直方向上面就會(huì)增加30個(gè)像素的間隙 //GUILayout.Button("Button3", GUILayout.Width(100), GUILayout.Height(50)); //GUILayout.Button("Button4", GUILayout.Width(100), GUILayout.Height(50)); //GUILayout.EndVertical(); #endregion #region 常用的GUI控件 #region GUI.Button //GUI.Button(new Rect(20, 20, 150, 30), "這是一個(gè)文字按鈕"); ////繪制紋理按鈕 //GUI.Button(new Rect(20, 60, 150, 30), texture);//texture是在unity上面Script腳本上面拖上圖片進(jìn)行賦值的 ////繪制一個(gè)帶圖片和文字按鈕 //GUIContent guic = new GUIContent("按鈕", texture); //GUI.Button(new Rect(20, 100, 150, 30), guic); ////設(shè)置按鈕的樣式 //GUIStyle guis = new GUIStyle(); //guis.fontSize = 23; //guis.alignment = TextAnchor.MiddleCenter; ////設(shè)置狀態(tài)樣式 //GUIStyleState guiss = new GUIStyleState(); //guiss.textColor = Color.white; //guiss.background = texture2D;//設(shè)置按鈕背景圖片,texture2D在編輯器上拖圖片賦值 //guis.normal = guiss;//設(shè)置按鈕正常顯示的狀態(tài) //GUIStyleState guissActive = new GUIStyleState(); //guissActive.textColor = Color.white; //guissActive.background = texture2DActive;//設(shè)置按鈕背景圖片,texture2D在編輯器上拖圖片賦值 //guis.active = guissActive;//設(shè)置鼠標(biāo)按下去按鈕上顯示的狀態(tài) //guis.hover = guissActive;//設(shè)置鼠標(biāo)放在按鈕上顯示的狀態(tài) //if (GUI.Button(new Rect(20, 140, 150, 30), "樣式按鈕", guis))//點(diǎn)擊后返回true //{ // Debug.Log("點(diǎn)擊了按鈕"); //} #endregion #region GUI.Label //GUI.color = Color.red;//全局設(shè)置顏色,設(shè)置后后面的控件都變?yōu)榧t色,直到重新設(shè)置顏色 //GUI.Label(new Rect(20, 180, 100, 50), "label1"); //GUI.color = Color.blue; //GUI.Label(new Rect(20, 200, 100, 50), "label2"); #endregion #region GUI.TextField GUI.PasswordField GUI.TextArea //userName = GUI.TextField(new Rect(10, 10, 100, 30), userName); //password = GUI.PasswordField(new Rect(10, 50, 100, 30), password,'*'); //remark = GUI.TextArea(new Rect(10, 100, 100, 30),remark); //if (GUI.Button(new Rect(10,150,50,30),"登錄")) //{ // Debug.Log(userName + "-"+password+"-"+remark); // if (userName.Equals("admin")&&password.Equals("123")) // { // isSuccess = true; // } // else // { // isSuccess = false; // } //} //if (isSuccess) //{ // GUI.Label(new Rect(10, 200, 100, 30), "登錄成功!"); //} //else //{ // GUI.Label(new Rect(10, 200, 100, 30), "登錄失??!"); //} #endregion #region GUI.Toolbar GUI.Toggle GUI.HorizontalSlider //Tab頁(yè),返回值為激活的按鈕的序號(hào),三個(gè)按鈕并排,select為0選中第一個(gè)按鈕 //select = GUI.Toolbar(new Rect(0, 0, 300, 50), select, new string[] { "功能一", "功能二", "功能三" }); //Debug.Log(select); //單選按鈕 //GUIStyle gs = new GUIStyle(); //GUIStyleState gss = new GUIStyleState(); //gss.textColor = Color.white; //gs.normal = gss; //gs.active = gss; //GUIContent contenxt = new GUIContent("開(kāi)關(guān)", bug1); //if (toggle1) //{ // contenxt.image = bug2; //} //// toggle = GUI.Toggle(new Rect(10, 10, 100, 30), toggle, "是否開(kāi)啟聲音"); //toggle1 = GUI.Toggle(new Rect(10, 10, 50, 50), toggle1, contenxt, gs); //GUI.Label(new Rect(10, 80, 100, 30), toggle1 + ""); //水平拖動(dòng)的Slider,h為Slider賦值 //h = GUI.HorizontalSlider(new Rect(0, 0, 100, 100), h, 0, 100); //Debug.Log(h); #endregion #region GUI.BeginScrollView GUI.BeginGroup GUI.Window GUI.SelectionGrid //開(kāi)始滾動(dòng)視圖 // public static Vector2 BeginScrollView(Rect position, Vector2 scrollPosition, Rect viewRect, bool alwaysShowHorizontal, bool alwaysShowVertical); //position 用于滾動(dòng)視圖在屏幕上矩形的位置 //scrollPosition 用來(lái)顯示滾動(dòng)位置 //viewRect 滾動(dòng)視圖內(nèi)使用的矩形 //vector2 = GUI.BeginScrollView(new Rect(0, 0, 200, 200), vector2, new Rect(0, 0, 200, 200), true, true); //GUI.Button(new Rect(0, 0, 50, 50),"Button"); //GUI.EndScrollView(); //開(kāi)始組 將控件都放在一組中,只要組變動(dòng),里面的控件都跟著變 //GUI.BeginGroup(new Rect(10, 100, 200, 400)); //GUI.Label(new Rect(10, 100, 100, 30), "群組視圖1"); //GUI.Button(new Rect(10, 130, 100, 30), "按鈕"); //GUI.EndGroup(); //GUI.BeginGroup(new Rect(200, 0, 300, 400)); //GUI.Label(new Rect(10, 100, 100, 30), "群組視圖2"); //GUI.Button(new Rect(10, 130, 100, 30), "按鈕"); //GUI.EndGroup(); //彈出窗口 //必須要把窗口的位置設(shè)置成全局變量,窗口里面內(nèi)容在回調(diào)函數(shù)里面寫(xiě) //rect1 = GUI.Window(0, rect1, win, "窗口"); //rect2 = GUI.Window(1, rect2, win, "窗口"); //選擇表格 //selGridId = GUI.SelectionGrid(new Rect(10, 10, 300, 200), selGridId, selString, 2); //Debug.Log(selGridId); #endregion #region GUILayout.BeginArea //區(qū)域就是無(wú)邊框的窗口,Button控件隨著區(qū)域移動(dòng) //GUILayout.BeginArea(new Rect(0, 50, 200, 200), "Area"); //GUI.Button(new Rect(0,0,100,50),"Button"); //GUILayout.EndArea(); #endregion #endregion } }
- 上一篇:motionbuilder動(dòng)作數(shù)據(jù)文件的導(dǎo)入 2020/3/15
- 下一篇:Unity3D跨屏幕、全屏顯示方法 2019/12/14