【Unity】OnGUI 代码生成UI


GUI.Box 盒子
GUI.Button 按钮
GUI.RepeatButton 按住会触发的按钮
GUI.Label 标签文本
GUI.TextField 单行文本框
GUI.TextArea 多行文本框
GUI.Toggle 单选 radio
GUI.Toolbar 单选 tab
GUI.SelectionGrid 单选 可以表格布局
GUI.HorizontalSlider 滑动条 水平方向
GUI.VerticalSlider 滑动条 垂直方向
GUI.BeginScrollView、GUI.EndScrollView() 滚动视图
GUI.HorizontalScrollbar 滚动条 水平方向
GUI.VerticalScrollbar 滚动条 垂直方向
GUI.Window 窗口
GUI.DragWindow 可以拖动,必须放在最下面一行
GUI.BeginGroup、GUI.EndGroup() 分组
GUI.changed 监视UI数据变化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TCP : MonoBehaviour
{
    private string textFieldString = "text field 0ne line";
    private string textAreaString = "text area \n multi line";
    private bool toggleBool = true;
    private int toolbarInt = 0;
    private string[] toolbarStrings = { "Toolbar1", "Toolbar12", "Toolbar3" };
    private int selectionGridInt = 0;
    private string[] selectionStrings = { "Grid1", "Grid2", "Grid3", "Grid4" };
    private float hSliderValue = 0.0f;
    private float vSliderValue = 0.0f;
    private float hScrollbarValue;
    private float vScrollbarValue;

    private Vector2 scrollViewVector = Vector2.zero;

    private Rect windowRect = new Rect(10, 340, 120, 50);
    private bool IsShowWindow = false;

    void OnGUI()
    {
        //滚动视图 BeginScrollView EndScrollView
        scrollViewVector = GUI.BeginScrollView(new Rect(5, 5, 300, 300), scrollViewVector, new Rect(0, 0, 500, 700));

        //盒子
        GUI.Box(new Rect(10, 10, 100, 90), "Box");

        //按钮
        if (GUI.Button(new Rect(10, 110, 80, 20), "Button"))
        {
            Debug.Log("Clicked");
        }

        //标签
        GUI.Label(new Rect(10, 140, 100, 30), "Label");

        //按住会触发的按钮
        if (GUI.RepeatButton(new Rect(10, 180, 100, 30), "RepeatButton"))
        {
            Debug.Log("pressing");
        }

        //单行文本框
        textFieldString = GUI.TextField(new Rect(10, 220, 200, 30), textFieldString);

        //多行文本框
        textAreaString = GUI.TextArea(new Rect(10, 260, 200, 60), textAreaString);

        //单选 radio
        toggleBool = GUI.Toggle(new Rect(10, 330, 100, 30), toggleBool, "Toggle");

        //单选 tab
        toolbarInt = GUI.Toolbar(new Rect(10, 370, 250, 30), toolbarInt, toolbarStrings);

        //单选 可以表格布局
        selectionGridInt = GUI.SelectionGrid(new Rect(10, 410, 300, 60), selectionGridInt, selectionStrings, 2);
      
        //滑动条 水平方向
        hSliderValue = GUI.HorizontalSlider(new Rect(10, 480, 100, 30), hSliderValue, 0.0f, 10.0f);

        //监视UI数据变化 GUI.changed
        //只监视该段代码以上的 GUI
        if (GUI.changed)
        {
            Debug.Log(hScrollbarValue);
            Debug.Log(IsShowWindow);
        }

        //滑动条 垂直方向
        vSliderValue = GUI.VerticalSlider(new Rect(10, 520, 30, 60), vSliderValue, 0.0f, 10.0f);

        //滚动条 水平方向
        hScrollbarValue = GUI.HorizontalScrollbar(new Rect(10, 590, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);

        //滚动条 垂直方向
        vScrollbarValue = GUI.VerticalScrollbar(new Rect(10, 630, 30, 60), vScrollbarValue, 1.0f, 0.0f, 10.0f);

        GUI.EndScrollView();


        if (GUI.Button(new Rect(10, 310, 120, 20), "Open my Window"))
        {
            IsShowWindow = true;
        }
        // 满足条件,展示窗口
        if (IsShowWindow)
        {
            windowRect = GUI.Window(0, windowRect, WindowFunction, "my Window");
        }

        //分组
        //组内元素的坐标是相对组元素的
        GUI.BeginGroup(new Rect(15, 415, 300, 300));

        GUI.Box(new Rect(0, 0, 100, 90), "Box");

        GUI.EndGroup();

    }

    //窗口内的元素定义和属性设定(只要窗口显示,就会执行)
    void WindowFunction(int windowID)
    {
        if (GUI.Button(new Rect(25, 25, 80, 20), "Close"))
        {
            IsShowWindow = false;
        }
        GUI.DragWindow(new Rect(0, 0, 120, 20));//可以拖动,必须放在最下面一行
    }
}

posted @ 2024-10-25 09:35  Sitar  阅读(1)  评论(0编辑  收藏  举报