UGUI-擴展 IMGUI
可借助多種方法利用和擴展 IMGUI 系統(tǒng),從而滿足您的需求。您可以混合并創(chuàng)建控件,并充分控制 GUI 用戶輸入的處理方式。
復合控件
GUI 中可能存在兩種類型的控件總是一起出現(xiàn)的情況。例如,假設正在創(chuàng)建具有多個水平滑動條 (Horizontal Slider) 的“角色創(chuàng)建 (Character Creation)”屏幕。所有這些滑動條 (Slider) 都需要一個標簽 (Label) 來進行標識,讓玩家知道自己正在調(diào)整什么。在這種情況下,可將每個?GUI.Label()?調(diào)用與?GUI.HorizontalSlider()?調(diào)用進行搭配,或者可創(chuàng)建一個同時包含 Label 和 Slider 的__復合控件__。
/* Label 和 Slider 復合控件 */
// JavaScript
var mySlider : float = 1.0;
function OnGUI()
{
? ? mySlider = LabelSlider(Rect(10, 100, 100, 20), mySlider, 5.0, "Label text here");
}
function LabelSlider(screenRect : Rect, sliderValue : float, sliderMaxValue : float, labelText : String) : float {
? ? GUI.Label(screenRect, labelText);
? ? screenRect.x += screenRect.width; // <- 將 Slider 推到 Label 的末尾
? ? sliderValue = GUI.HorizontalSlider(screenRect, sliderValue, 0.0, sliderMaxValue);
? ? return sliderValue;
}
// C#
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);
? ? ? ? // <- 將 Slider 推到 Label 的末尾
? ? ? ? screenRect.x += screenRect.width;
? ? ? ? sliderValue = GUI.HorizontalSlider(screenRect, sliderValue, 0.0f, sliderMaxValue);
? ? ? ? return sliderValue;
? ? }
}
在此示例中,調(diào)用?LabelSlider()?并傳遞正確的參數(shù)將提供與水平滑動條 (Horizontal Slider) 配對的標簽 (Label)。創(chuàng)建復合控件時,必須記住需要在函數(shù)末尾返回正確的值以使其具有交互性。

靜態(tài)復合控件
通過使用__靜態(tài)__函數(shù),可以創(chuàng)建自成一體的完整復合控件集合。這樣,就不必在需要使用函數(shù)的同一腳本中聲明該函數(shù)。
/* 此腳本名為 CompoundControls */
// JavaScript
static function LabelSlider(screenRect : Rect, sliderValue : float, sliderMaxValue : float, labelText : String) : float {
? ? GUI.Label(screenRect, labelText);
? ? screenRect.x += screenRect.width; // <- 將 Slider 推到 Label 的末尾
? ? sliderValue = GUI.HorizontalSlider(screenRect, sliderValue, 0.0, sliderMaxValue);
? ? return sliderValue;
}
// C#
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);
? ? ? ? // <- 將 Slider 推到 Label 的末尾
? ? ? ? screenRect.x += screenRect.width;
? ? ? ? sliderValue = GUI.HorizontalSlider(screenRect, sliderValue, 0.0f, sliderMaxValue);
? ? ? ? return sliderValue;
? ? }
}
通過將以上示例保存在名為?CompoundControls?的腳本中,只需輸入?CompoundControls.LabelSlider()?并提供參數(shù),即可從任何其他腳本調(diào)用?LabelSlider()?函數(shù)。
精心設計的復合控件
可使用復合控件實現(xiàn)出色的創(chuàng)造性??砂慈魏蜗矚g的方式對復合控件進行排列和分組。以下示例將創(chuàng)建可重復使用的 RGB Slider。
/* RGB Slider 復合控件 */
// JavaScript
var myColor : Color;
function OnGUI()
{
? ? myColor = RGBSlider(Rect(10, 10, 200, 10), myColor);
}
function RGBSlider(screenRect : Rect, rgb : Color) : Color {
? ? rgb.r = GUI.HorizontalSlider(screenRect, rgb.r, 0.0, 1.0);
? ? screenRect.y += 20; // <- 將下一個控件向下移動一點以避免重疊
? ? rgb.g = GUI.HorizontalSlider(screenRect, rgb.g, 0.0, 1.0);
? ? screenRect.y += 20; // <- 將下一個控件向下移動一點以避免重疊
? ? rgb.b = GUI.HorizontalSlider(screenRect, rgb.b, 0.0, 1.0);
? ? return rgb;
}
// C#
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);
? ? ? ? // <- 將下一個控件向下移動一點以避免重疊
? ? ? ? screenRect.y += 20;
? ? ? ? rgb.g = GUI.HorizontalSlider(screenRect, rgb.g, 0.0f, 1.0f);
? ? ? ? // <- 將下一個控件向下移動一點以避免重疊
? ? ? ? screenRect.y += 20;
? ? ? ? rgb.b = GUI.HorizontalSlider(screenRect, rgb.b, 0.0f, 1.0f);
? ? ? ? return rgb;
? ? }
}

現(xiàn)在讓我們構建一些位于彼此之上的復合控件,從而演示如何在復合控件中使用其他復合控件。為此,我們將創(chuàng)建如上所示的新 RGB Slider,但我們將使用 LabelSlider 來執(zhí)行此操作。這樣我們將始終有一個標簽顯示哪個滑動條對應哪種顏色。
/* RGB Label Slider 復合控件 */
// JavaScript
var myColor : Color;
function OnGUI()
{
? ? myColor = RGBLabelSlider(Rect(10, 10, 200, 20), myColor);
}
function RGBLabelSlider(screenRect : Rect, rgb : Color) : Color {
? ? rgb.r = CompoundControls.LabelSlider(screenRect, rgb.r, 1.0, "Red");
? ? screenRect.y += 20; // <- 將下一個控件向下移動一點以避免重疊
? ? rgb.g = CompoundControls.LabelSlider(screenRect, rgb.g, 1.0, "Green");
? ? screenRect.y += 20; // <- 將下一個控件向下移動一點以避免重疊
? ? rgb.b = CompoundControls.LabelSlider(screenRect, rgb.b, 1.0, "Blue");
? ? return rgb;
}
// C#
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");
? ? ? ? // <- 將下一個控件向下移動一點以避免重疊
? ? ? ? screenRect.y += 20;
? ? ? ? rgb.g = CompoundControls.LabelSlider(screenRect, rgb.g, 1.0f, "Green");
? ? ? ? // <- 將下一個控件向下移動一點以避免重疊
? ? ? ? screenRect.y += 20;
? ? ? ? rgb.b = CompoundControls.LabelSlider(screenRect, rgb.b, 1.0f, "Blue");
? ? ? ? return rgb;
? ? }
}
