(3/∞) Lable and Button components in UnityGUI

UnityGUI provides us with many static methods for displaying the UI interface. Since UnityGUI is a code-driven GUI, we have no way to create it directly in the Scene window of the game, but to set it in the form of code.

If you want to call the static method in UnityGUI, you can only call it in the OnGUI function in the class that inherits the Mono script. This function is executed every frame, and we generally only execute GUI-related interface drawing and operation logic in it. This function is in Executed before OnDisable and after LateUpdate .

 private void OnGUI()
    {
        //在其中书写GUI相关代码 即可显示GUI内容
    }

The GUI provides us with a lot of common basic controls, which are all static functions in the GUI public class, and the parameters are similar.

The parameters that almost all exist are :

Position parameter: Rect parameter xy position wh represents size

display text: string parameter

Image information: Texture parameters

Comprehensive information: GUIContent parameters

Custom style: GUIStyle parameter

Each control has multiple overloads, which are the permutations and combinations of various parameters, and the position information and display information are necessary parameter contents.

For example:

Lable (text) control:

We can use the text control in this way, pass in an instance of Rect to Lable or directly declare a public Rect field externally, and initialize it from the Inspector window to achieve the purpose of debugging the UI when the Game is running.

        GUI.Label(new Rect(0, 0, 200, 20),"王嘉然,认识王嘉然",style);
        GUI.Label(rect, texture);

Label control also has many overloaded methods

        //综合使用
        GUI.Label(rect2, content);
        Debug.Log(GUI.tooltip);

        //自定义样式
        //Lable 函数存在很多重载,其中有一个参数就是传递一个Style进去

The following is the status of each parameter in the Inspector window

    Rect parameter

Content parameter

 

 style parameter

The Button control is basically the same as the Lable, but the Button control has a return value of bool type, which can be used to determine whether the button is clicked. code show as below:

        //基本使用
        //在按钮范围内 按下鼠标再抬起鼠标 才算一次点击 才会返回true
        if (GUI.Button(buttonRect, buttonContent, buttonStyle))
        {
            print("按钮被点击了");
        }
        //只要在长按按钮范围内 按下鼠标 就会一直返回true
        if (GUI.RepeatButton(buttonRect, buttonContent, buttonStyle))
        {
            print("长按按钮被点击");
        }

 

Guess you like

Origin blog.csdn.net/qq_52690206/article/details/127040217