C# GUI

GUI 界面

界面类是Unity手工定制的GUI的接口

GUI.backgroundColor 背景颜色

全局染色由GUI渲染的所有背景元素,得到乘以颜色

void OnGUI() {

    GUI.backgroundColor = Color.yellow;

    GUI.Button(new Rect(10, 10, 70, 30), "A button");

}

GUI.BeginGroup 开始组

开始组,必须配套以EndGroup结束关闭容器

void OnGUI() {

    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.EndGroup();

}

GUI.BeginScrollView 开始滚动视图

在你的GUI里,开始一个滚动视图, 注意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();

}

GUI.Box 盒子

在界面布局创建盒子。盒子包含文本,图像或者带有工具提示的这些的组合,通过使用GUIContent参数。你也可以使用GUIStyle去调整盒子中所有的物体的布局,文本颜色和其他属性

void OnGUI() {

    GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "This is a box");

}

GUI.BringWindowToBack 使窗口到后面

使一个特定的窗口到浮动窗口的后面

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));

}

GUI.BringWindowToFront 使窗口到前面

使一个特定的窗口到浮动窗口的前面

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), "Bring to front"))

        GUI.BringWindowToFront(1);

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

}

void DoMySecondWindow(int windowID) {

    if (GUI.Button(new Rect(10, 20, 100, 20), "Bring to front"))

        GUI.BringWindowToFront(0);

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

}

GUI.Button 按钮

创建一个单次按下按钮。用户点击按钮事件立即触发

public Texture btnTexture;

void OnGUI() {

    if (!btnTexture) {

        Debug.LogError("Please assign a texture on the inspector");

        return;

    }

    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");

}

GUI.changed 改变

如果任何控制按钮改变输入数据的值那么返回true

public string stringToEdit = "Modify me.";

void OnGUI() {

    stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);

    if (GUI.changed)

        Debug.Log("Text field has changed.");

}

GUI.color 颜色

全局GUI染色,将影响背景和文本颜色

void OnGUI() {

    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");

}

GUI.contentColor 内容颜色

全局染色由GUI渲染的所有文本,得到乘以颜色

void OnGUI() {

    GUI.contentColor = Color.yellow;

    GUI.Button(new Rect(10, 10, 70, 30), "A button");

}

GUI.depth 深度

当前执行的GUI行为的深度排序

public static int depth;

GUI.DragWindow 拖动窗口

创建一个可拖动窗口

public Rect windowRect = new Rect(20, 20, 120, 50);

void OnGUI() {

    windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");

}

void DoMyWindow(int windowID) {

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

}

GUI.DrawTexture 绘制纹理

在矩形内绘制一个纹理

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);

}

GUI.DrawTextureWithTexCoords 使用纹理坐标绘制纹理

在给定的纹理坐标矩形范围内绘制纹理。使用该函数为了裁剪或者铺盖图像到指定的矩形内部

public static void DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords);

GUI.enabled 是否启用?

判断GUI是否启用了

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");

}

GUI.EndGroup 结束组

结束组,和开数组BeginGroup成对使用

void OnGUI() {

    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.EndGroup();

}

GUI.EndScrollView 结束滚动视图

结束被开始的滚动视图,注意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();

}

GUI.FocusControl 焦点控件

移动键盘焦点到被命名的控件

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");

}

GUI.FocusWindow 焦点窗口

使一个窗口成为活动窗口

private Rect windowRect = new Rect(20, 80, 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);

}

GUI.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);

    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");

}

GUI.HorizontalScrollbar 水平滚动条

创建水平滚动条。滚动条是能通过滚动来浏览文档,大多数情况下,你可以使用scrollView代替

public float hSbarValue;

void OnGUI() {

    hSbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), hSbarValue, 1.0F, 0.0F, 10.0F);

}

GUI.HorizontalSlider 水平滑动条

水平滑动条,用户能拖动改变最小和最大值之间

public float hSliderValue = 0.0F;

void OnGUI() {

    hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0F, 10.0F);

}

GUI.Label 标签

在屏幕上创建一个文本或者纹理标签

void OnGUI() {

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

}

GUI.matrix 变换矩阵

GUI变换矩阵

void Example() {

    print(GUI.matrix);

}

GUI.ModalWindow 模式窗口

显示模式窗口

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, string text);

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, Texture image);

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, GUIContent content);

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, string text, GUIStyle style);

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, Texture image, GUIStyle style);

public static Rect ModalWindow(int id, Rect clientRect, GUI.WindowFunction func, GUIContent content, GUIStyle style);

GUI.PasswordField 密码字段

创建文本字段,用户可以编辑密码

public string passwordToEdit = "My Password";

void OnGUI() {

    passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);

}

GUI.RepeatButton

创建一个按钮,只要用户按着不放,将一直被激活

public Texture btnTexture;

void OnGUI() {

    if (!btnTexture) {

        Debug.LogError("Please assign a texture on the inspector");

        return;

    }

    if (GUI.RepeatButton(new Rect(10, 10, 50, 50), btnTexture))

        Debug.Log("Clicked the button with an image");

    if (GUI.RepeatButton(new Rect(10, 70, 50, 30), "Click"))

        Debug.Log("Clicked the button with text");

}

GUI.ScrollTo 滚动至

滚动scrollviews到position指定的位置,通俗来说就是把内容滚动到指定的坐标

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();

}

GUI.SelectionGrid 选择表格

创建一个网格按钮

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, 2);

}

GUI.SetNextControlName 设置下一个控件名字

设置下一个控件的名字

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");

}

GUI.skin

全局皮肤使用

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);

}

GUI.TextArea 文本区域

创建多行文本区域,用户可以编辑字符串

public string stringToEdit = "Hello World\nI've got 2 lines...";

void OnGUI() {

    stringToEdit = GUI.TextArea(new Rect(10, 10, 200, 100), stringToEdit, 200);

}

GUI.TextField 文本字段

创建单行文本字段,用户可以编辑字符串

public string stringToEdit = "Hello World";

void OnGUI() {

    stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);

}

GUI.Toggle 开关按钮

创建on/off开关按钮,也就是类似通常说的单选按钮

public Texture aTexture;

private bool toggleTxt = false;

private bool toggleImg = false;

void OnGUI() {

    if (!aTexture) {

        Debug.LogError("Please assign a texture in the inspector.");

        return;

    }

    toggleTxt = GUI.Toggle(new Rect(10, 10, 100, 30), toggleTxt, "A Toggle text");

    toggleImg = GUI.Toggle(new Rect(10, 50, 50, 50), toggleImg, aTexture);

}

GUI.Toolbar 工具栏

创建工具栏

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);

}

GUI.tooltip 工具提示

控制鼠标当前通过对象的提升信息,或具有键盘焦点

void OnGUI() {

    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);

}

GUI.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();

}

GUI.VerticalScrollbar 垂直滚动条

创建垂直滚动条。滚动条是能通过滚动来浏览文档,大多数情况下,你可以使用scrollView代替

public float vSbarValue;

void OnGUI() {

    vSbarValue = GUI.VerticalScrollbar(new Rect(25, 25, 100, 30), vSbarValue, 1.0F, 10.0F, 0.0F);

}

GUI.VerticalSlider 垂直滑条

垂直滑条指用户可以在最小和最大值之间拖动或者改变

public float vSliderValue = 0.0F;

void OnGUI() {

    vSliderValue = GUI.VerticalSlider(new Rect(25, 25, 100, 30), vSliderValue, 10.0F, 0.0F);

}

GUI.Window 窗口

创建一个弹出窗口

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");

}

GUI.WindowFunction 窗口功能

在窗口内回调绘制GUI(和GUI.Window一起使用)

public delegate void WindowFunction(int id);

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值