Version: 6000.3
语言: 中文
IMGUI 布局模式
GUI 皮肤(IMGUI 系统)

扩展 IMGUI

有多种方法可以利用和扩展 IMGUI 系统来满足您的需求。可以混合和创建控件,并且在决定如何处理用户输入到 GUI 方面具有很大的影响力。

复合控件

在 GUI 中,可能会出现两种类型的控件总是一起出现的情况。例如,也许你正在创建一个角色创建屏幕,其中包含多个水平滑块。所有这些滑块都需要一个标签来识别它们,以便玩家知道它们正在调整什么。在这种情况下,您可以与对 GUI 的每次调用进行合作。Label() 调用 GUI。HorizontalSlider() 的 Compound Control,或者你可以创建一个 Compound Control,其中同时包含 Label 和 Slider。

// Label and Slider Compound Control

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {
        
    private float mySlider = 1.0f;
    
    void OnGUI () {
        mySlider = LabelSlider (new Rect (10, 100, 100, 20), mySlider, 5.0f, "Label text here");
    }
    
    float LabelSlider (Rect screenRect, float sliderValue, float sliderMaxValue, string labelText) {
        GUI.Label (screenRect, labelText);
    
        // <- Push the Slider to the end of the Label
        screenRect.x += screenRect.width; 
    
        sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue);
        return sliderValue;
    }

}

在此示例中,调用 LabelSlider() 并传递正确的参数将提供与水平滑块配对的 Label。编写复合控件时,您必须记住在函数末尾返回正确的值以使其具有交互性。

上述复合控件始终创建这对控件
上述复合控件始终创建这对控件

静态复合控件

通过使用静态函数,你可以创建自己的自包含复合控件的完整集合。这样,您就不必在要使用它的同一脚本中声明您的函数。

// This script is called CompoundControls
using UnityEngine;
using System.Collections;

public class CompoundControls : MonoBehaviour {     
    
    public static float LabelSlider (Rect screenRect, float sliderValue, float sliderMaxValue, string labelText) {
        GUI.Label (screenRect, labelText);
    
        // <- Push the Slider to the end of the Label
        screenRect.x += screenRect.width; 
    
        sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue);
        return sliderValue;
    }

}

通过将上述示例保存在名为 CompoundControls 的脚本中,您只需键入 CompoundControls.LabelSlider() 并提供参数,即可从任何其他脚本调用 LabelSlider() 函数。

精心设计的化合物对照

您可以使用复合控件获得非常大的创意。它们可以按照您喜欢的任何方式排列和分组。以下示例创建了一个可重用的 RGB 滑块。

// RGB Slider Compound Control

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {
        
    public Color myColor;
    
    void OnGUI () {
        myColor = RGBSlider (new Rect (10,10,200,10), myColor);
    }
    
    Color RGBSlider (Rect screenRect, Color rgb) {
        rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0f, 1.0f);
    
        // <- Move the next control down a bit to avoid overlapping
        screenRect.y += 20; 
        rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0f, 1.0f);
    
        // <- Move the next control down a bit to avoid overlapping
        screenRect.y += 20; 
    
        rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0f, 1.0f);
        return rgb;
    }
}
上面示例创建的 RGB 滑块
上面示例创建的 RGB 滑块

现在,让我们将复合控件构建在彼此之上,以演示如何在其他复合控件中使用复合控件。为此,我们将创建一个新的 RGB 滑块,如上所示,但我们将使用 LabelSlider 来执行此作。这样,我们将始终有一个标签告诉我们哪个滑块对应于哪种颜色。

// RGB Label Slider Compound Control

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {
        
    public Color myColor;
    
    void OnGUI () {
        myColor = RGBSlider (new Rect (10,10,200,30), myColor);
    }
    
    Color RGBSlider (Rect screenRect, Color rgb) {
        rgb.r = CompoundControls.LabelSlider (screenRect, rgb.r, 1.0f, "Red");
    
        // <- Move the next control down a bit to avoid overlapping
        screenRect.y += 20; 
        rgb.g = CompoundControls.LabelSlider (screenRect, rgb.g, 1.0f, "Green");
    
        // <- Move the next control down a bit to avoid overlapping
        screenRect.y += 20; 
    
        rgb.b = CompoundControls.LabelSlider (screenRect, rgb.b, 1.0f, "Blue");
        
        return rgb;
    }   
    
}
由上述代码创建的复合 RGB 标签滑块
由上述代码创建的复合 RGB 标签滑块
IMGUI 布局模式
GUI 皮肤(IMGUI 系统)