OnGUI in Unity

1. The development history of the UI system in unity
(1) Legacy GUI: the earliest UI technology of unity, it only has two components, text and picture (GUIText, GUITexture), and cooperates with mouse events to realize interface UI interaction, which is basically no longer used now , which can only be seen in some old projects.
(2) OnGUI: It may be that Legacy GUI has too few functions. Unity has built-in OnGUI. OnGUI needs to use code to create UI, set the position and size of UI, etc.
Disadvantages: The UI created by OnGUI will be called every frame, which will consume a lot of main thread CPU time and GC allocation, which consumes performance. The UI created by the GUI needs to be visible when it is running, and invisible when it is not running. GUI classes can only be used within the OnGUI method.
(3) NGUI: Because OnGUI is very cumbersome to use, someone developed an NGUI plug-in, which can better control and manage the UI, and makes UI in the form of "what you see is what you get", so everyone starts to use NGUI plug-in to create UI . At present, the interface UI of most domestic games is still implemented using NGUI plug-ins.
(4) UGUI: The UI system launched by Unity since version 4.6 is also the form of "what you see is what you get" to make UI. It is still the most commonly used.


2. The main control of OnGUI
(1) Label: The label is non-interactive. It is for display only, it cannot be clicked or otherwise moved
(2) Button: Interactive button. When you click, no matter how long the mouse remains low, it will respond once, and when you release the mouse, the response will occur immediately
(3) TextField: Interactive, editable single-line input box
(4) TextArea: Interactive, editable multi-line Input area
(5) Toggle: checkbox
(6) Toolbar: essentially a row of buttons, any number of buttons can be defined on the toolbar
(7) HorizontalSlider: horizontal slider
(8) VerticalSlider: vertical slider
(9 ) HorizontalScrollbar: Horizontal Scrollbar
(10) VerticalScrollbar: Vertical Scrollbar
(11) ScrollViews: Scroll View


3. Example of using OnGUI:

Mount the script and run unity

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

public class GUITest : MonoBehaviour
{
    bool isToggle=true;
    private int toolbarInt = 0;
    private string[] toolbarStrings = { "Toolbar1", "Toolbar2", "Toolbar3" };
    private float hSliderValue = 0.0f;
    private float vSliderValue = 5.0f;
    private float hScrollbarValue;
    private float vScrollbarValue;
    private Vector2 scrollViewVector = Vector2.zero;
    private string innerText = "滚动视图";
    // Start is called before the first frame update
    void Start()
    {
        
    }
    private void OnGUI()
    {
        //标签,前两个数表示x,y的位置,后两个数表示宽高
        GUI.Label(new Rect(20, 10, 100, 20), "标签");
        //按钮
        GUI.Button(new Rect(20, 40, 100, 20), "按钮");
        //输入框
        GUI.TextField(new Rect(20, 70, 200, 20), "输入框");
        //文本区域
        GUI.TextArea(new Rect(20, 100, 200, 50), "文本区域");
        //Toggle
        GUI.Toggle(new Rect(20, 160, 100, 30),isToggle,"Toggle");
        //Toolbar
        GUI.Toolbar(new Rect(300, 10, 200, 30), toolbarInt, toolbarStrings);
        //HorizontalSlider
        hSliderValue=GUI.HorizontalSlider(new Rect(300, 50, 100, 30), hSliderValue, 0.0f, 10.0f);
        //VerticalSlider
        vSliderValue = GUI.VerticalSlider(new Rect(300, 90, 100, 30), vSliderValue, 10.0f, 0.0f);
        //HorizontalScrollbar
        hScrollbarValue = GUI.HorizontalScrollbar(new Rect(300, 130, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);
        //VerticalScrollbar
        vScrollbarValue = GUI.VerticalScrollbar(new Rect(300, 170, 100, 30), vScrollbarValue, 1.0f, 10.0f, 0.0f);
        //ScrollViews,前面Rect的100,100表示显示区域的大小,后面Rect的400,400表示能输入内容的区域大小,scrollViewVector表示滑动条的位置
        scrollViewVector = GUI.BeginScrollView(new Rect(300, 210, 100, 100), scrollViewVector, new Rect(0, 0, 400, 400));
        innerText = GUI.TextArea(new Rect(0, 0, 400, 400), innerText);
        GUI.EndScrollView();
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

 For more details, please refer to the official website: https://docs.unity3d.com/Manual/gui-Controls.html

Conclusion: The tree that embraces is born at the end of the hair; the nine-story platform starts from the pile of soil; the journey of a thousand miles begins with a single step.

おすすめ

転載: blog.csdn.net/falsedewuxin/article/details/129945994
おすすめ