UNITY3D GUI組件(jiàn)使用例子
2019/12/22      點(diǎn)擊(jī):
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), "點擊按鈕");        //使(shǐ)用(yòng)DragWindow啟用窗口拖動
        GUI.DragWindow();    }    void OnGUI()
    {
        #region GUILayout布局
        ////GUILayout采用線性(xìng)布局,類似於StackPanel,默認是縱向布局。通過GUILayout.BeginHorizontal();
        ////開(kāi)啟和GUILayout.EndHorizontal()結束(shù)一個橫向排列區域(yù),同理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();
        ////如果(guǒ)嫌(xián)控件太擠,可以使用GUILayout.Space(30);增加若幹像素的間隙。
        //GUILayout.Space(30);//Button3和Button1在垂直方向上麵就會(huì)增加(jiā)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腳本上麵拖上圖片進行賦值(zhí)的
        ////繪製一(yī)個帶圖片和文字(zì)按鈕
        //GUIContent guic = new GUIContent("按鈕", texture);
        //GUI.Button(new Rect(20, 100, 150, 30), guic);        ////設置按鈕(niǔ)的樣(yàng)式
        //GUIStyle guis = new GUIStyle();
        //guis.fontSize = 23;
        //guis.alignment = TextAnchor.MiddleCenter;        ////設置狀態樣式
        //GUIStyleState guiss = new GUIStyleState();
        //guiss.textColor = Color.white;
        //guiss.background = texture2D;//設置按鈕背景圖片,texture2D在編輯器(qì)上拖圖片賦值
        //guis.normal = guiss;//設置按鈕正常顯示的狀態
        //GUIStyleState guissActive = new GUIStyleState();
        //guissActive.textColor = Color.white;
        //guissActive.background = texture2DActive;//設置按鈕背景圖片,texture2D在編輯器上拖圖片賦值
        //guis.active = guissActive;//設置鼠標按下去按鈕上顯示的狀態
        //guis.hover = guissActive;//設置鼠標放在按鈕上顯示(shì)的狀態
        //if (GUI.Button(new Rect(20, 140, 150, 30), "樣式按鈕", guis))//點(diǎn)擊後返回true
        //{
        //    Debug.Log("點擊了按鈕");
        //} 
        #endregion        #region GUI.Label
        //GUI.color = Color.red;//全局設置顏色,設置後後麵的控件都變為紅色,直到重新設置顏色(sè)
        //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), "登錄成(chéng)功(gōng)!");
        //}
        //else
        //{
        //    GUI.Label(new Rect(10, 200, 100, 30), "登錄失敗!");
        //}
        #endregion        #region GUI.Toolbar GUI.Toggle  GUI.HorizontalSlider
        //Tab頁,返回值為激活的(de)按(àn)鈕的序號,三(sān)個按鈕並排,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("開關", bug1);
        //if (toggle1)
        //{
        //    contenxt.image = bug2;
        //}
        //// toggle = GUI.Toggle(new Rect(10, 10, 100, 30), toggle, "是否開(kāi)啟(qǐ)聲音");
        //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
        //開始滾動視圖
        //  public static Vector2 BeginScrollView(Rect position, Vector2 scrollPosition, Rect viewRect, bool alwaysShowHorizontal, bool alwaysShowVertical);
        //position 用於滾動視圖在屏幕上矩形的位置
        //scrollPosition 用來顯示滾動位置
        //viewRect 滾動視圖內使用的矩形
        //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();        //開始組 將控件都放在(zài)一組中,隻要組變(biàn)動,裏麵的控件都跟著變
        //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), "群(qún)組視圖2");
        //GUI.Button(new Rect(10, 130, 100, 30), "按鈕");
        //GUI.EndGroup();        //彈出窗口
        //必須要把窗口的位置(zhì)設置成全局變量,窗口裏麵內容在回調函數裏麵寫
        //rect1 = GUI.Window(0, rect1, win, "窗口");
        //rect2 = GUI.Window(1, rect2, win, "窗口");        //選擇(zé)表格(gé)
        //selGridId = GUI.SelectionGrid(new Rect(10, 10, 300, 200), selGridId, selString, 2);
        //Debug.Log(selGridId); 
        #endregion        #region GUILayout.BeginArea
        //區域就是無邊框的窗口,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動作數據文件(jiàn)的導入 2020/3/15
- 下一篇:Unity3D跨屏幕、全屏顯(xiǎn)示方(fāng)法 2019/12/14



