Version: 6000.3
语言: 中文
IMGUI 基础知识
自定义 IMGUI 控件

IMGUI 控件

IMGUI 控件类型

您可以创建许多不同的 IMGUI 控件。此部分列出了所有可用的显示和交互式控件。还有其他影响控件布局的 IMGUI 函数,指南的布局部分中介绍了这些函数。

标签

标签是非交互式的。它仅供展示。它不能被点击或以其他方式移动。它最好仅显示信息。

/* GUI.Label example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    void OnGUI () 
    {
        GUI.Label (new Rect (25, 25, 100, 30), "Label");
    }

}


示例代码创建的标签
示例代码创建的标签

按钮

按钮是典型的交互式按钮。无论鼠标保持按压多长时间,单击时它都会响应一次。松开鼠标按钮后立即发生响应。

基本用法

在 UnityGUI 中,单击按钮时将返回 true。要在单击 Button 时执行一些代码,请包装 GUI.if 语句中的按钮函数。if 语句中是单击 Button 时将执行的代码。

/* GUI.Button example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    void OnGUI () 
    {
        if (GUI.Button (new Rect (25, 25, 100, 30), "Button")) 
        {
            // This code is executed when the Button is clicked
        }
    }

}


示例代码创建的 Button
示例代码创建的 Button

重复按钮

RepeatButton 是常规 Button 的变体。不同之处在于,RepeatButton 将在鼠标按钮保持按下状态的每一帧中做出响应。这允许您创建单击并按住功能。

基本用法

在 UnityGUI 中,RepeatButtons 将为单击的每一帧返回 true。要在单击 Button 时执行一些代码,请包装 GUI.if 语句中的 RepeatButton 函数。if 语句中是将在 RepeatButton 保持单击状态时执行的代码。

/* GUI.RepeatButton example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    void OnGUI () 
    {
        if (GUI.RepeatButton (new Rect (25, 25, 100, 30), "RepeatButton")) 
        {
            // This code is executed every frame that the RepeatButton remains clicked
        }
    }

}


示例代码创建的重复按钮
示例代码创建的重复按钮

文本字段

TextField 控件是一个交互式、可编辑的单行字段,其中包含文本字符串。

基本用法

TextField 将始终显示一个字符串。您必须提供要在 TextField 中显示的字符串。对字符串进行编辑时,TextField 函数将返回编辑后的字符串。

/* GUI.TextField example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private string textFieldString = "text field";
    
    void OnGUI () 
    {
        textFieldString = GUI.TextField (new Rect (25, 25, 100, 30), textFieldString);
    }

}


示例代码创建的 TextField
示例代码创建的 TextField

文本区域

TextArea 控件是一个交互式、可编辑的多行区域,其中包含文本字符串。

基本用法

TextArea 将始终显示一个字符串。您必须提供要在 TextArea 中显示的字符串。对字符串进行编辑时,TextArea 函数将返回编辑后的字符串。

/* GUI.TextArea example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour
{
                    
    private string textAreaString = "text area";
    
    void OnGUI ()
    {
        textAreaString = GUI.TextArea (new Rect (25, 25, 100, 30), textAreaString);
    }

}


示例代码创建的 TextArea
示例代码创建的 TextArea

切换

切换 允许用户打开或关闭选项的复选框。更多信息
请参阅术语表
Control 创建一个具有持久开/关状态的复选框。用户可以通过单击它来更改状态。

基本用法

“切换开/关”状态由 true/false 布尔值表示。您必须提供布尔值作为参数,以使 Toggle 表示实际状态。如果单击 Toggle 函数,它将返回一个新的布尔值。为了捕获这种交互性,您必须分配布尔值以接受 Toggle 函数的返回值。

/* GUI.Toggle example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
    private bool toggleBool = true;
    
    void OnGUI () 
    {
        toggleBool = GUI.Toggle (new Rect (25, 25, 100, 30), toggleBool, "Toggle");
    }
}


示例代码创建的 Toggle
示例代码创建的 Toggle

工具栏

工具栏Unity 编辑器顶部的一排按钮和基本控件,允许您以各种方式(例如缩放、平移)与编辑器交互。更多信息
请参阅术语表
控件本质上是一行按钮。工具栏上的一个按钮一次只能处于活动状态,并且它将保持活动状态,直到单击不同的按钮。此行为模拟典型工具栏的行为。您可以在工具栏上定义任意数量的按钮。

基本用法

工具栏中的活动按钮通过整数进行跟踪。您必须在函数中提供整数作为参数。若要使工具栏具有交互性,必须将整数分配给函数的返回值。您提供的内容数组中的元素数将决定工具栏中显示的按钮数。

/* GUI.Toolbar example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private int toolbarInt = 0;
    private string[] toolbarStrings = {"Toolbar1", "Toolbar2", "Toolbar3"};
    
    void OnGUI () 
    {
        toolbarInt = GUI.Toolbar (new Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);
    }

}


示例代码创建的工具栏
示例代码创建的工具栏

选择网格

SelectionGrid 控件是一个多行工具栏。您可以确定网格中的列数和行数。一次只能激活一个按钮。

基本用法

SelectionGrid 中的活动 Button 通过整数进行跟踪。您必须在函数中提供整数作为参数。若要使 SelectionGrid 具有交互性,必须将整数分配给函数的返回值。您提供的内容数组中的元素数将决定 SelectionGrid 中显示的按钮数。您还可以通过函数参数指定列数。

/* GUI.SelectionGrid example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private int selectionGridInt = 0;
    private string[] selectionStrings = {"Grid 1", "Grid 2", "Grid 3", "Grid 4"};
    
    void OnGUI () 
    {
        selectionGridInt = GUI.SelectionGrid (new Rect (25, 25, 300, 60), selectionGridInt, selectionStrings, 2);
    
    }

}


示例代码创建的 SelectionGrid
示例代码创建的 SelectionGrid

水平滑块

HorizontalSlider Control 是一个典型的水平滑动旋钮,可以拖动以在预定的最小值和最大值之间更改值。

基本用法

“滑块”旋钮的位置存储为浮点。要显示旋钮的位置,请将该浮点数作为函数中的参数之一提供。还有两个附加值用于确定最小值和最大值。如果希望滑块旋钮可调节,请将滑块值 float 指定为“滑块”函数的返回值。

/* Horizontal Slider example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private float hSliderValue = 0.0f;
    
    void OnGUI () 
    {
        hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
    }

}


由示例代码创建的水平滑块
由示例代码创建的水平滑块

垂直滑块

VerticalSlider 控件是一个典型的垂直滑动旋钮,可以拖动以在预定的最小值和最大值之间更改值。

基本用法

“滑块”旋钮的位置存储为浮点。要显示旋钮的位置,请将该浮点数作为函数中的参数之一提供。还有两个附加值用于确定最小值和最大值。如果希望滑块旋钮可调节,请将滑块值 float 指定为“滑块”函数的返回值。

/* Vertical Slider example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private float vSliderValue = 0.0f;
    
    void OnGUI () 
    {
        vSliderValue = GUI.VerticalSlider (new Rect (25, 25, 100, 30), vSliderValue, 10.0f, 0.0f);
    }

}


由示例代码创建的垂直滑块
由示例代码创建的垂直滑块

水平滚动条

HorizontalScrollbar 控件类似于滑块控件,但在视觉上类似于 Web 浏览器或文字处理器的滚动元素。此控件用于导航ScrollView一种 UI 控件,用于在可视区域中显示大量控件,您可以使用滚动条查看这些控件。更多信息
请参阅术语表
控制。

基本用法

水平滚动条的实现方式与水平滑块相同,但有一个例外:还有一个附加参数用于控制滚动条旋钮本身的宽度。

/* Horizontal Scrollbar example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
    private float hScrollbarValue;
    
    void OnGUI () 
    {
        hScrollbarValue = GUI.HorizontalScrollbar (new Rect (25, 25, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);
    }

}


示例代码创建的水平滚动条
示例代码创建的水平滚动条

垂直滚动条

VerticalScrollbar 控件类似于 Slider 控件,但在视觉上类似于 Web 浏览器或文字处理器的 Scrolling 元素。此控件用于导航 ScrollView 控件。

基本用法

垂直滚动条的实现方式与垂直滑块相同,但有一个例外:还有一个附加参数用于控制滚动条旋钮本身的高度。

/* Vertical Scrollbar example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour
{
                    
    private float vScrollbarValue;
    
    void OnGUI ()
    {
        vScrollbarValue = GUI. VerticalScrollbar (new Rect (25, 25, 100, 30), vScrollbarValue, 1.0f, 10.0f, 0.0f);
    }
}


示例代码创建的垂直滚动条
示例代码创建的垂直滚动条

滚动视图

ScrollView 是显示更大控件集的可视区域的控件。

基本用法

ScrollView 需要两个 Rect 作为参数。第一个 Rect 定义屏幕上可查看的 ScrollView 区域的位置和大小。第二个 Rect 定义可视区域内包含的空间的大小。如果可视区域内的空间大于可视区域,则滚动条将根据需要显示。您还必须分配并提供一个 2D 矢量,用于存储所显示的可视区域的位置。

/* ScrollView example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private Vector2 scrollViewVector = Vector2.zero;
    private string innerText = "I am inside the ScrollView";
    
    void OnGUI () 
    {
        // Begin the ScrollView
        scrollViewVector = GUI.BeginScrollView (new Rect (25, 25, 100, 100), scrollViewVector, new Rect (0, 0, 400, 400));
    
        // Put something inside the ScrollView
        innerText = GUI.TextArea (new Rect (0, 0, 400, 400), innerText);
    
        // End the ScrollView
        GUI.EndScrollView();
    }

}


示例代码创建的 ScrollView
示例代码创建的 ScrollView

窗口是控件的可拖动容器。点击时,它们可能会接收和失去焦点。因此,它们的实现方式与其他控件略有不同。每个窗口都有一个 id 号,其内容在单独的函数中声明,当窗口具有焦点时调用该函数。

基本用法

Windows 是唯一需要附加功能才能正常工作的控件。必须提供要为窗口执行的 ID 号和函数名称。在 Window 函数中,您可以创建实际行为或包含的控件。

/* Window example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour 
{
                    
    private Rect windowRect = new Rect (20, 20, 120, 50);
    
    void OnGUI ()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
    }
    
    void WindowFunction (int windowID) 
    {
        // Draw any Controls inside the window here
    }

}


示例代码创建的窗口
示例代码创建的窗口

GUI.更改

要检测用户是否在 GUI 中执行了任何作(单击按钮、拖动滑块等),请从脚本中读取 GUI.changed 值。当用户执行某些作时,此值将设置为 true,从而可以轻松验证用户输入。

一个常见的方案是工具栏,你希望根据单击工具栏中的按钮来更改特定值。您不希望在每次调用 OnGUI() 时才将值分配给 OnGUI(),仅当单击其中一个按钮时。

/* GUI.changed example */

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour
{
                    
    private int selectedToolbar = 0;
    private string[] toolbarStrings = {"One", "Two"};
    
    void OnGUI () 
    {
        // Determine which button is active, whether it was clicked this frame or not
        selectedToolbar = GUI.Toolbar (new Rect (50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);
    
        // If the user clicked a new Toolbar button this frame, we'll process their input
        if (GUI.changed)
        {
            Debug.Log("The toolbar was clicked");
    
            if (0 == selectedToolbar)
            {
                Debug.Log("First button was clicked");
            }
            else
            {
                Debug.Log("Second button was clicked");
            }
        }
    }

}


如果用户作之前放置了任何 GUI 控件,则 GUI.changed 将返回 true。

IMGUI 基础知识
自定义 IMGUI 控件