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