Unity IMGUI Controls(fixed layout)使用参考

前言:
Unity原生实现的GUI系统在调试中还是比较方便的,所以这里从官方文档里提取出一些常用的操作,方便自己日后使用的时候快速的查看。
这里介绍的是fixed layout,固定布局,稍后会记录Automatic Layout

IMGUI的全称是 Immediate Mode GUI system(IMGUI),即时模式的GUI,
完全由代码驱动(即没有visual design 可视化编辑,除非你扩展),通过
一个特殊的函数OnGUI来驱动,每一个MonoBehaviour中都可以实现
OnGUI函数,每帧堵附调用,就像update一样,前提是当前的MonoBehaviour是可见的

IMGUI的用途:
官方明确说到 mainly intended as a tool for programmers.
主要用于程序员开发一些辅助性的工具:

The Immediate Mode GUI system is commonly used for:

1.Creating in-game debugging displays and tools.
在游戏内,创建debugging调试窗口,监控某些数值,或是作弊码

2.Creating custom inspectors for script components.
为components创建自定义的inspectors,该功能使用率极高,自定义编辑器的时候也会经常用到

3.Creating new editor windows and tools to extend Unity itself.
扩展unity功能,比如开发了第三方的插件,或是一些工具类的,如打包,生成预设等等,通过在一个新的弹出浮动的window窗口操作,IMGUI用于在window中创建
GUI元素。

GUI.html

Static Variables说明:

backgroundColor:

image.png
GUI.backgroundColor = Color.green;
        GUI.Button(new Rect(10, 10, 70, 30), "A button");
image.png

color:

GUI.color = Color.yellow;
        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
        GUI.Box(new Rect(10, 50, 50, 50), "A BOX");
        GUI.Button(new Rect(10, 110, 70, 30), "A button");

contentColor:
改变所有文本的颜色

image.png
GUI.contentColor = Color.yellow;
        GUI.Button(new Rect(10, 10, 70, 30), "A button");

depth:
深度,当有多个类同时实现了OnGUI方法时,由depth来确定层级

image.png
// Makes this button go back in depth over the example2 class one.
class example1 extends MonoBehaviour {
    static var guiDepth : int = 0;
    function OnGUI() {
        GUI.depth = guiDepth;
        if(GUI.RepeatButton(Rect(0,0,100,100), "GoBack")) {
            guiDepth = 1;
            example2.guiDepth = 0;
        }
    }
}

// Makes this button go back in depth over the example1 class one.
class example2 extends MonoBehaviour {
    static var guiDepth : int = 1;
    function OnGUI() {
        GUI.depth = guiDepth;
        if(GUI.RepeatButton(Rect(50,50,100,100), "GoBack")) {
            guiDepth = 1;
            example1.guiDepth = 0;
        }
    }
}

enabled:

GUI响应标识,设置false,则从设置行起,后面的所有GUI都不会响应,并且显示半透明状

image.png
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public bool allOptions = true;
    public bool extended1 = true;
    public bool extended2 = true;
    void OnGUI() {
        allOptions = GUI.Toggle(new Rect(0, 0, 150, 20), allOptions, "Edit All Options");
        GUI.enabled = allOptions;
        extended1 = GUI.Toggle(new Rect(20, 20, 130, 20), extended1, "Extended Option 1");
        extended2 = GUI.Toggle(new Rect(20, 40, 130, 20), extended2, "Extended Option 2");
        GUI.enabled = true;
        if (GUI.Button(new Rect(0, 60, 150, 20), "Ok"))
            print("user clicked ok");
        
    }
}

skin:
设置全局皮肤,首先创建skin文件,Assets/Create/GUI skin,可以针对不同控件的不同属性进行控制,当然这个通常不会使用,除非你有需求把界面做得美观一些,比如发布的第三方插件等

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public GUISkin[] s1;
    private float hSliderValue = 0.0F;
    private float vSliderValue = 0.0F;
    private float hSValue = 0.0F;
    private float vSValue = 0.0F;
    private int cont = 0;
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space))
            cont++;
        
    }
    void OnGUI() {
        GUI.skin = s1[cont % s1.Length];
        if (s1.Length == 0) {
            Debug.LogError("Assign at least 1 skin on the array");
            return;
        }
        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
        GUI.Box(new Rect(10, 50, 50, 50), "A BOX");
        if (GUI.Button(new Rect(10, 110, 70, 30), "A button"))
            Debug.Log("Button has been pressed");
        
        hSliderValue = GUI.HorizontalSlider(new Rect(10, 150, 100, 30), hSliderValue, 0.0F, 10.0F);
        vSliderValue = GUI.VerticalSlider(new Rect(10, 170, 100, 30), vSliderValue, 10.0F, 0.0F);
        hSValue = GUI.HorizontalScrollbar(new Rect(10, 210, 100, 30), hSValue, 1.0F, 0.0F, 10.0F);
        vSValue = GUI.VerticalScrollbar(new Rect(10, 230, 100, 30), vSValue, 1.0F, 10.0F, 0.0F);
    }
}

tooltip:
提示信息,鼠标移动到指定控制上后,tooltip字符串会被赋值,并通过Label显示出来


image.png

GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", "This is the tooltip"));
GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);

*GUIContent类:
public GUIContent (string text, string tooltip);//tooltip

tooltip也可以用于实现鼠标移进和移同的消息系统:

public string lastTooltip = " ";
    void OnGUI() {
        GUILayout.Button(new GUIContent("Play Game", "Button1"));
        GUILayout.Button(new GUIContent("Quit", "Button2"));
        if (Event.current.type == EventType.Repaint && GUI.tooltip != lastTooltip) {
            if (lastTooltip != "")
                SendMessage(lastTooltip + "OnMouseOut", SendMessageOptions.DontRequireReceiver);

            if (GUI.tooltip != "")
                SendMessage(GUI.tooltip + "OnMouseOver", SendMessageOptions.DontRequireReceiver);

            lastTooltip = GUI.tooltip;
        }
    }

    void Button1OnMouseOver() {
        Debug.Log("Play game got focus");
    }
    void Button2OnMouseOut() {
        Debug.Log("Quit lost focus");
    }

*SendMessage用于在Component中发送数据,他会调用附加在gameobject上的每一个Monobehaviour类中的指定方法。
比如foo01.cs和foo02.cs都挂在同一个gameobject下,而且均定义了方法print方法,则这两个方法都会被调用。

public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
public void SendMessage(string methodName, SendMessageOptions options);

methodName:方法名
value:方法匹配的参数,可以不传参数,调用无参的方法
SendMessageOptions:
DontRequireReceiver
RequireReceiver
设置当没有组件接到收消息时,即没有匹配的方法,是否要抛出异常,DontRequireReceiver即便是没有收到,也不会有任何的错误提示。

注意:SendMessage只搜索可视的对象。

SendMessage的使用,印象中是在iOS oc中回传参数给gameobject时使用过。

Static Functions说明:

BeginGroup&EndGroup:
创建一个组别Group,所有的控件都会被限制在组内,超出的部分会被裁剪掉,如果控件比较多,可以方便的通过移动组来进行移动。

GUI.BeginGroup(new Rect(Screen.width / 2 - 400, Screen.height / 2 - 300, 800, 600));
        GUI.Box(new Rect(0, 0, 800, 600), "This box is now centered! - here you would put your main menu");
        GUI.Box(new Rect(50, 550, 200,200), "This box is now centered! - here you would put your main menu");//cliped
        GUI.EndGroup();

基础构造函数:

public static void BeginGroup([Rect](Rect.html) position);
public static void BeginGroup([Rect](Rect.html) position, string text);

position->Rectangle on the screen to use for the group.
text->Text to display on the group.

image.png

BeginScrollView&EndScrollView:
构造一个滚动的视图

public Vector2 scrollPosition = Vector2.zero;
    void OnGUI() {
        scrollPosition = GUI.BeginScrollView(new Rect(10, 300, 100, 100), scrollPosition, new Rect(0, 0, 220, 200));
        GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
        GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
        GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
        GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
        GUI.EndScrollView();

    }
image.png

Box:(GUI.Box.html)

GUI.Box(new Rect(0, 0, 100,100),"this is a box");

创建一个方框,但他并不会约束其它的控件。

image.png

BringWindowToBack:
window窗体最后显示,传入指定窗体的window id,会将当前窗体显示在所有窗体的后面。

private Rect windowRect = new Rect(20, 20, 120, 50);
    private Rect windowRect2 = new Rect(80, 20, 120, 50);
    void OnGUI() {
        
        windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
        windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
    }

    void DoMyFirstWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
            GUI.BringWindowToBack(0);

        GUI.DragWindow(new Rect(0, 0, 10000, 20));
    }
    void DoMySecondWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
            GUI.BringWindowToBack(1);

        GUI.DragWindow(new Rect(0, 0, 10000, 20));
    }
image.png

*window使用的频率还是很高的。
GUI.Window(0, windowRect, DoMyFirstWindow, "First");
指定窗体的ID,Rect,窗体绘制方法,以及窗体名称

BringWindowToFront:
对应上面BringWindowToBack,传入窗体的ID,最前显示当前的window,代码和截图就不提供了。

Button:
必备的,创建按钮

public static bool Button(Rect position, string text);
public static bool Button(Rect position, Texture image);
public static bool Button(Rect position, GUIContent content);
public static bool Button(Rect position, string text, GUIStyle style);
public static bool Button(Rect position, Texture image, GUIStyle style);
public static bool Button(Rect position, GUIContent content, GUIStyle style);

 if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
            Debug.Log("Clicked the button with an image");
        
        if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
            Debug.Log("Clicked the button with text");

DragWindow:
创建可以拖拽的窗体,默认window是无法拖拽的,在绘制窗体方法中插入指定代码即可

private Rect windowRect = new Rect(20, 20, 120, 50);
    private Rect windowRect2 = new Rect(80, 20, 120, 50);
    void OnGUI() {
        
        windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
        windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
    }

    void DoMyFirstWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
            GUI.BringWindowToBack(0);

        GUI.DragWindow(new Rect(0, 0, 120, 20));
    }
    void DoMySecondWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
            GUI.BringWindowToBack(1);

        GUI.DragWindow(new Rect(0, 0, 10000, 20));
    }

GUI.DragWindow(new Rect(0, 0, 10000, 20));

指定可拖拽的区域。
如果想要拖拽整个window,不传参数即可。

GUI.DragWindow();

DrawTexture:
在矩形框内,绘制一张纹理

public static void DrawTexture([Rect](Rect.html) position, [Texture](Texture.html) image);

public static void DrawTexture([Rect](Rect.html) position, [Texture](Texture.html) image, [ScaleMode](ScaleMode.html) scaleMode);

public static void DrawTexture([Rect](Rect.html) position, [Texture](Texture.html) image, [ScaleMode](ScaleMode.html) scaleMode, bool alphaBlend);

public static void DrawTexture([Rect](Rect.html) position, [Texture](Texture.html) image, [ScaleMode](ScaleMode.html) scaleMode, bool alphaBlend, float imageAspect);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Texture aTexture;
    void OnGUI() {
        if (!aTexture) {
            Debug.LogError("Assign a Texture in the inspector.");
            return;
        }
        GUI.DrawTexture(new Rect(10, 10, 60, 60), aTexture, ScaleMode.ScaleToFit, true, 10.0F);
    }
}

ScaleMode:
指定缩放的方式
alphaBlend:
alpha混合,默认是enabled
imageAspect:
图像缩放比,即w/h的比值

image.png

DrawTextureWithTexCoords:
绘制一张纹理,通过给定的点,这些点决定了在aspect ratio不同时,如何绘制,使用该方法可以实现一些剪切和平铺的效果。

FocusControl:
将焦点聚焦在已命名的Control组件上。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public string username = "username";
    public string pwd = "a pwd";
    void OnGUI() {
        GUI.SetNextControlName("MyTextField");
        username = GUI.TextField(new Rect(10, 10, 100, 20), username);
        pwd = GUI.TextField(new Rect(10, 40, 100, 20), pwd);
        if (GUI.Button(new Rect(10, 70, 80, 20), "Move Focus"))
            GUI.FocusControl("MyTextField");
        
    }
}

FocusWindow:
让指定的窗体获得焦点。

private Rect windowRect = new Rect(20, 20, 120, 50);
    private Rect windowRect2 = new Rect(20, 80, 120, 50);
    void OnGUI() {
        windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
        windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
    }
    void DoMyFirstWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Focus other"))
            GUI.FocusWindow(1);

    }
    void DoMySecondWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Focus other"))
            GUI.FocusWindow(0);

    }

GetNameOfFocusedControl:
获取当前焦点所在控件的名称

public string login = "username";
    public string login2 = "no action here";
    void OnGUI() {
        GUI.SetNextControlName("user");
        login = GUI.TextField(new Rect(10, 10, 130, 20), login);
        login2 = GUI.TextField(new Rect(10, 40, 130, 20), login2);
        Debug.Log (GUI.GetNameOfFocusedControl ());
        if (Event.current.isKey && Event.current.keyCode == KeyCode.Return && GUI.GetNameOfFocusedControl() == "user")
            Debug.Log("Login");

        if (GUI.Button(new Rect(150, 10, 50, 20), "Login"))
            Debug.Log("Login");

    }

*点击login框后,GetNameOfFocusedControl的名字就变成了user
*KeyCode.Return是回车键

HorizontalScrollbar:
创建水平的滚动条(卷轴),可以结合Label显示滚动的数值,通过可以使用scrollview来实现

public static float HorizontalScrollbar(Rect position, float value, float size, float leftValue, float rightValue);
public static float HorizontalScrollbar(Rect position, float value, float size, float leftValue, float rightValue, GUIStyle style);

public float hSbarValue;
    void OnGUI() {
        hSbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), hSbarValue, 2.0F, 0.0F, 10.0F);
    }


position Rectangle on the screen to use for the scrollbar.
value The position between min and max.//当前滚动的值
size How much can we see?//控制把手儿的大小
leftValue The value at the left end of the scrollbar.//最小值
rightValue The value at the right end of the scrollbar.//最大值

style The style to use for the scrollbar background. If left out, the horizontalScrollbar style from the current GUISkin is used.

*对应的是VerticalScrollbar,垂直的滚动条

HorizontalSlider:
水平的滑动块

public float hSliderValue = 0.0F;
    void OnGUI() {
        hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0F, 10.0F);
    }

*参数同上
*对应的是VerticalSlider 垂直的滑动块

Label:
标签,显示无交互的字符串或是纹理。

public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent content);
public static void Label(Rect position, string text, GUIStyle style);
public static void Label(Rect position, Texture image, GUIStyle style);
public static void Label(Rect position, GUIContent content, GUIStyle style);


 void OnGUI() {
        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
    }

public Texture2D textureToDisplay;
    void OnGUI() {
        GUI.Label(new Rect(10, 40, textureToDisplay.width, textureToDisplay.height), textureToDisplay);
    }

ModalWindow:
类似于Window,显示一个窗体,但该窗体会位于所有的GUI元素之上,并保证是所有输入和事件的唯一接收者,
当ModalWindow显示时,其它控件不会再处理输入,一次只能显示一个ModalWindow.

*代码就不提供了,和window是一样的

PasswordField:
密码文本域,针对密码的情况

public static string PasswordField(Rect position, string password, char maskChar);
public static string PasswordField(Rect position, string password, char maskChar, int maxLength)
public static string PasswordField(Rect position, string password, char maskChar, GUIStyle style);
public static string PasswordField(Rect position, string password, char maskChar, int maxLength, GUIStyle style);

position Rectangle on the screen to use for the text field.
password Password to edit. The return value of this function should be assigned back to the string as shown in the example.
maskChar Character to mask the password with.//掩码字符
maxLength The maximum length of the string. If left out, the user can type for ever and ever.//最大的长度
style The style to use. If left out, the textField style from the current GUISkin is used.

RepeatButton:
重复按钮,重复发生的动作,当用户点击按钮后,RepeatButton 返回true

public static bool RepeatButton(Rect position, string text);
public static bool RepeatButton(Rect position, Texture image);
public static bool RepeatButton(Rect position, GUIContent content);
public static bool RepeatButton(Rect position, string text, GUIStyle style);
public static bool RepeatButton(Rect position, Texture image, GUIStyle style);
public static bool RepeatButton(Rect position, GUIContent content, GUIStyle style);

同样支持text和texture.

if (GUI.RepeatButton(new Rect(10, 70, 50, 30), "Click"))
            Debug.Log("Clicked the button with text");

ScrollTo:
滚动scrollview到指定的位置

  public Vector2 scrollPos = Vector2.zero;
    void OnGUI() {
        scrollPos = GUI.BeginScrollView(new Rect(10, 10, 100, 50), scrollPos, new Rect(0, 0, 220, 10));
        if (GUI.Button(new Rect(0, 0, 100, 20), "Go Right"))
            GUI.ScrollTo(new Rect(120, 0, 100, 20));
        
        if (GUI.Button(new Rect(120, 0, 100, 20), "Go Left"))
            GUI.ScrollTo(new Rect(0, 0, 100, 20));
        
        GUI.EndScrollView();
    }

SelectionGrid:
创建一组按钮网格

public static int SelectionGrid(Rect position, int selected, string[] texts, int xCount);
public static int SelectionGrid(Rect position, int selected, Texture[] images, int xCount);
public static int SelectionGrid(Rect position, int selected, GUIContent[] content, int xCount);
public static int SelectionGrid(Rect position, int selected, string[] texts, int xCount, GUIStyle style);
public static int SelectionGrid(Rect position, int selected, Texture[] images, int xCount, GUIStyle style);
public static int SelectionGrid(Rect position, int selected, GUIContent[] contents, int xCount, GUIStyle style);

也可以支持texture网格

public int selGridInt = 0;
    public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4"};
    void OnGUI() {
        selGridInt = GUI.SelectionGrid(new Rect(25, 25, 100, 30), selGridInt, selStrings, 3);
    }

参数:
xCount->水平方向可以排列几个button。

SetNextControlName:
设置下一个控制组件的名称,前面提到过

他会影响他的下一行代码的控件

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public string login = "username";
    public string login2 = "no action here";
    void OnGUI() {
        GUI.SetNextControlName("user");
        login = GUI.TextField(new Rect(10, 10, 130, 20), login);
        login2 = GUI.TextField(new Rect(10, 40, 130, 20), login2);
        if (Event.current.Equals(Event.KeyboardEvent("return")) && GUI.GetNameOfFocusedControl() == "user")
            Debug.Log("Login");
        
        if (GUI.Button(new Rect(150, 10, 50, 20), "Login"))
            Debug.Log("Login");
        
    }
}

点击login输入框时,GetNameOfFocusedControl才会返回user
GUI.SetNextControlName("user");对下面的login进行命名,配合FocusControl
使用可高亮全选当前的文本域。

 public string login = "username";
    public string login2 = "no action here";
    void OnGUI() {
        GUI.SetNextControlName("user");
        login = GUI.TextField(new Rect(10, 10, 130, 20), login);
        login2 = GUI.TextField(new Rect(10, 40, 130, 20), login2);
        if (Event.current.Equals(Event.KeyboardEvent("return")) && GUI.GetNameOfFocusedControl() == "user")
            Debug.Log("Login");
        
        if (GUI.Button(new Rect(150, 10, 50, 20), "Login"))
            Debug.Log("Login");
        
    }

TextArea:
创建可编辑多行字符串的文本域。在Attribute特性中,通过属性也可以实现该效果

public static string TextArea(Rect position, string text);
public static string TextArea(Rect position, string text, int maxLength);
public static string TextArea(Rect position, string text, GUIStyle style);
public static string TextArea(Rect position, string text, int maxLength, GUIStyle style);
public string stringToEdit = "Hello World\nI've got 2 lines...";
    void OnGUI() {
        stringToEdit = GUI.TextArea(new Rect(10, 10, 200, 100), stringToEdit, 200);
    }

参数:
maxLength->文本最大长度

TextField:
创建一个可单行编辑的字符串。字符串的在Inspector中的默认样式

public static string TextField(Rect position, string text);
public static string TextField(Rect position, string text, int maxLength);
public static string TextField(Rect position, string text, GUIStyle style);
public static string TextField(Rect position, string text, int maxLength, GUIStyle style);
 public string stringToEdit = "Hello World";
    void OnGUI() {
        stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
    }

Toggle:
创建开关。

public static bool Toggle(Rect position, bool value, string text);
public static bool Toggle(Rect position, bool value, Texture image);
public static bool Toggle(Rect position, bool value, GUIContent content);
public static bool Toggle(Rect position, bool value, string text, GUIStyle style);
public static bool Toggle(Rect position, bool value, Texture image, GUIStyle style);
public static bool Toggle(Rect position, bool value, GUIContent content, GUIStyle style);
public Texture aTexture;
    private bool toggleTxt = false;
    private bool toggleImg = false;
    void OnGUI() {  
        toggleTxt = GUI.Toggle(new Rect(10, 10, 100, 30), toggleTxt, "A Toggle text");
        toggleImg = GUI.Toggle(new Rect(10, 50, 50, 50), toggleImg, aTexture);
    }

Toolbar:
创建工具条

public static int Toolbar(Rect position, int selected, string[] texts);
public static int Toolbar(Rect position, int selected, Texture[] images);
public static int Toolbar(Rect position, int selected, GUIContent[] content);
public static int Toolbar(Rect position, int selected, string[] texts, GUIStyle style);
public static int Toolbar(Rect position, int selected, Texture[] images, GUIStyle style);
public static int Toolbar(Rect position, int selected, GUIContent[] contents, GUIStyle style);
ToolBar.png
public int toolbarInt = 0;
    public string[] toolbarStrings = new string[] {"Toolbar1", "Toolbar2", "Toolbar3"};
    void OnGUI() {
        toolbarInt = GUI.Toolbar(new Rect(25, 25, 250, 30), toolbarInt, toolbarStrings);
        Debug.Log (toolbarInt);

    }

UnfocusWindow:
取消所有的窗口焦点

private Rect windowRect = new Rect(20, 20, 120, 50);
    private Rect windowRect2 = new Rect(20, 80, 120, 50);
    void OnGUI() {
        windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
        windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
    }
    void DoMyFirstWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "UnFocus"))
            GUI.UnfocusWindow();
        
    }
    void DoMySecondWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "UnFocus"))
            GUI.UnfocusWindow();
        
    }

Window:
创建一个弹出的window窗体

public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, string text);
public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, Texture image);
public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, GUIContent content);
public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, string text, GUIStyle style);
public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, Texture image, GUIStyle style);
public static Rect Window(int id, Rect clientRect, GUI.WindowFunction func, GUIContent title, GUIStyle style);
image.png
public Rect windowRect = new Rect(20, 20, 120, 50);
    void OnGUI() {
        windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
    }
    void DoMyWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
            print("Got a click");

    }

你可以创建多个Window弹出窗体,只需要保证他们的id是不同的。

public Rect windowRect0 = new Rect(20, 20, 120, 50);
    public Rect windowRect1 = new Rect(20, 100, 120, 50);
    void OnGUI() {
        windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window");
        windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window");
    }
    void DoMyWindow(int windowID) {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
            print("Got a click in window " + windowID);

        GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    }

隐藏window的显示,只需要在GUI方法中,屏蔽调用即可。

public bool doWindow0 = true;
    void DoWindow0(int windowID) {
        GUI.Button(new Rect(10, 30, 80, 20), "Click Me!");
    }
    void OnGUI() {
        doWindow0 = GUI.Toggle(new Rect(10, 10, 100, 20), doWindow0, "Window 0");
        if (doWindow0)
            GUI.Window(0, new Rect(110, 10, 200, 60), DoWindow0, "Basic Window");

    }

到此为止,如果大家发现有什么不对的地方,欢迎指正,共同提高,感谢您的阅读!

编辑于2018.7.23 雕刻咖啡(北苑店)

最近疫苗的事件闹得沸沸扬扬,简直突破人类的底线!!! 作恶者千刀万剐都难解心头之恨,再看看A股暴跌(那几天我还在俄罗斯看世界杯),区块链割韭菜(EOS的忠实持有者,已经当那钱丢了),房价上涨,贷款利率不断上调,刚需再遭一刀切,幼儿园的事件没有过去多久,就再也没有人谈起,疫苗的事件.... 最近挺sad,昨天看到一则微博里关于评价疫苗事件的,被末尾的一段话刺激到了,“问题不是在改革中出现的,问题是在改革停滞中停出来的!“,他何止是说现在的社会事件,我回想创业这两年的经历,不也是如此吗!表面看上去在努力的经营,努力的思考,加班加点做项目,但改变的决心并不够强烈,轻慢,拖延,就像温水上的青蛙,享受着那最舒服的温度,我们总愿意去做让我们感觉到舒服的事儿,但这样往往却对我们不利,离开创业的公司有5个月了,稍后会写一写做些总结,以此来纪念曾经奋斗的日子,也要铭记犯过的错误,教训和成长。

你可能感兴趣的:(Unity IMGUI Controls(fixed layout)使用参考)