Unity OnGUI 的可視化編輯


GUI

  Unity 的 GUI 主要分成兩種制作方式:

  一種是以 Component 命令的方式直接附給場景物體使用;

  像 GUITextureGUIText,它們的建立及調整相當方便,不用在執行期就能看得到,而且因為繼承自 Component,所以也可以直接使用 transformgameObject 等變數內容。

  另一種則是在 OnGUI 中撰寫的 GUI、GUILayout 等;

  這些可以配合 GUISkinGUIStyle 靈活運用制作各種表現方式的使用者介面,可惜的是它必須一行一行的撰寫程式碼,而且只有在游戲執行期才會呈現出來,變成制作時只能倚重程式設計人員撰寫,調整時必須執行游戲把調整結果的位置及寬高等信息記錄下來,再另行修改;

OnGUI

  OnGUIUnity 中通過代碼驅動的 GUI 系統,主要用來創建調試工具、創建自定義屬性面板、創建新的 Editor 窗口和工具達到擴展編輯器效果。

  OnGUI 在布局上,坐標系原點在屏幕左上角,所以不建議使用在項目 UI 當中。

Box矩形框

1     void OnGUI()
2     {
3         GUI.Box(new Rect(10, 10, 100, 90), "這是一個矩形框");
4     }

Button按鈕

1     void OnGUI()
2     {
3         if (GUI.Button(new Rect(20, 40, 80, 20), "按鈕"))
4         {
5             Debug.Log("按鈕點擊");
6         }
7     }

Label標簽

  標簽是非交互式的,無法單擊或以其他方式移動,最好只進行信息的顯示。

1     void OnGUI()
2     {
3         GUI.Label(new Rect(25, 25, 150, 30), "這是一條展示信息。");
4     }

RepeatButton按鈕

  按住后會重復執行單擊操作的按鈕:點擊按鈕時,每一幀執行一次。

1     void OnGUI()
2     {
3         if (GUI.RepeatButton(new Rect(25, 25, 150, 30), "RepeatButton按鈕"))
4         {
5             Debug.Log("重復執行!");
6         }
7     }

TextField單行輸入框

1     private string textFieldString = "請輸入... ...";
2 
3     void OnGUI()
4     {
5         textFieldString = GUI.TextField(new Rect(25, 25, 100, 30), textFieldString);
6     }

TextArea多行輸入框

1     private string textAreaString = "請輸入第一條內容: \n請輸入第二條內容:";
2 
3     void OnGUI()
4     {
5         textAreaString = GUI.TextArea(new Rect(25, 25, 150, 60), textAreaString);
6     }

Toggle單選框

1     private bool toggleBool = true;
2 
3     void OnGUI()
4     {
5         toggleBool = GUI.Toggle(new Rect(25, 25, 100, 30), toggleBool, "單選框");
6     }

Toolbar工具欄

1     private int toolbarInt = 0;
2     private string[] toolbarStrings = { "第一頁", "第二頁", "第三頁" };
3 
4     void OnGUI()
5     {
6         toolbarInt = GUI.Toolbar(new Rect(25, 25, 250, 30), toolbarInt, toolbarStrings);
7     }

SelectionGrid網格選擇形式

1     private int selectionGridInt = 0;
2     private string[] selectionStrings = { "網格_1", "網格_2", "網格_3", "網格_4" };
3 
4     void OnGUI()
5     {
6         selectionGridInt = GUI.SelectionGrid(new Rect(25, 25, 300, 60), selectionGridInt, selectionStrings, 2);
7     }

HorizontalSlider水平滑動條

1     private float hSliderValue = 0.0f;
2 
3     void OnGUI()
4     {
5         hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 200, 30), hSliderValue, 0.0f, 10.0f);
6     }

VerticalSlider垂直滑動條

1     private float vSliderValue = 0.0f;
2 
3     void OnGUI()
4     {
5         vSliderValue = GUI.VerticalSlider(new Rect(25, 25, 100, 50), vSliderValue, 10.0f, 0.0f);
6     }

HorizontalScrollbar水平滾動條

1     private float hScrollbarValue;
2 
3     void OnGUI()
4     {
5         hScrollbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 200, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);
6     }

VerticalScrollbar垂直滾動條

1     private float vScrollbarValue;
2 
3     void OnGUI()
4     {
5         vScrollbarValue = GUI.VerticalScrollbar(new Rect(25, 25, 100, 50), vScrollbarValue, 1.0f, 10.0f, 0.0f);
6     }

ScrollView滾動視圖

1     private Vector2 scrollViewVector = Vector2.zero;
2     private string innerText = "這是一個一條很長長長長長長長長長長長長長長長的信息。";
3 
4     void OnGUI()
5     {
6         scrollViewVector = GUI.BeginScrollView(new Rect(25, 25, 200, 100), scrollViewVector, new Rect(0, 0, 400, 400));
7         innerText = GUI.TextArea(new Rect(0, 0, 400, 400), innerText);
8         GUI.EndScrollView();
9     }

Window窗口

  控件的可拖動容器。

 1     private Rect windowRect = new Rect(20, 20, 150, 50);
 2 
 3     void OnGUI()
 4     {
 5         windowRect = GUI.Window(0, windowRect, WindowFunction, "Window窗口");
 6     }
 7 
 8     void WindowFunction(int windowID)
 9     {
10         Debug.Log("WindowFunction");
11     }

BeginGroup/EndGroup分組

 1     private string textFieldString_1 = "請輸入... ...";
 2     private string textFieldString_2 = "請輸入... ...";
 3 
 4     void OnGUI()
 5     {
 6         GUI.BeginGroup(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 50, 300, 300));
 7         GUI.Box(new Rect(0, 0, 200, 200), "組合界面");
 8         textFieldString_1 = GUI.TextField(new Rect(0, 30, 200, 30), textFieldString_1);
 9         textFieldString_2 = GUI.TextField(new Rect(0, 70, 200, 30), textFieldString_2);
10         GUI.Button(new Rect(50, 120, 100, 30), "點擊按鈕");
11         GUI.EndGroup();
12     }

  

***| 以上內容僅為學習參考、學習筆記使用 |***


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM