UNITY3D GUI組件使用例子
2019/12/22??????點擊:
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), "點擊按鈕"); //使用DragWindow啟用窗口拖動 GUI.DragWindow(); } void OnGUI() { #region GUILayout布局 ////GUILayout采用線性布局,類似于StackPanel,默認是縱向布局。通過GUILayout.BeginHorizontal(); ////開啟和GUILayout.EndHorizontal()結(jié)束一個橫向排列區(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在垂直方向上面就會增加30個像素的間隙 //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), "這是一個文字按鈕"); ////繪制紋理按鈕 //GUI.Button(new Rect(20, 60, 150, 30), texture);//texture是在unity上面Script腳本上面拖上圖片進行賦值的 ////繪制一個帶圖片和文字按鈕 //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è)置鼠標按下去按鈕上顯示的狀態(tài) //guis.hover = guissActive;//設(shè)置鼠標放在按鈕上顯示的狀態(tài) //if (GUI.Button(new Rect(20, 140, 150, 30), "樣式按鈕", guis))//點擊后返回true //{ // Debug.Log("點擊了按鈕"); //} #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頁,返回值為激活的按鈕的序號,三個按鈕并排,select為0選中第一個按鈕 //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("開關(guān)", bug1); //if (toggle1) //{ // contenxt.image = bug2; //} //// toggle = GUI.Toggle(new Rect(10, 10, 100, 30), toggle, "是否開啟聲音"); //toggle1 = GUI.Toggle(new Rect(10, 10, 50, 50), toggle1, contenxt, gs); //GUI.Label(new Rect(10, 80, 100, 30), toggle1 + ""); //水平拖動的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 //開始滾動視圖 // public static Vector2 BeginScrollView(Rect position, Vector2 scrollPosition, Rect viewRect, bool alwaysShowHorizontal, bool alwaysShowVertical); //position 用于滾動視圖在屏幕上矩形的位置 //scrollPosition 用來顯示滾動位置 //viewRect 滾動視圖內(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(); //開始組 將控件都放在一組中,只要組變動,里面的控件都跟著變 //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ù)里面寫 //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ū)域就是無邊框的窗口,Button控件隨著區(qū)域移動 //GUILayout.BeginArea(new Rect(0, 50, 200, 200), "Area"); //GUI.Button(new Rect(0,0,100,50),"Button"); //GUILayout.EndArea(); #endregion #endregion } }
- 上一篇:motionbuilder動作數(shù)據(jù)文件的導(dǎo)入 2020/3/15
- 下一篇:Unity3D跨屏幕、全屏顯示方法 2019/12/14