필요에 따라 IMGUI 시스템을 활용하고 확장하는 방법은 다양합니다. 사용자는 컨트롤을 혼합하고 생성할 수 있으며, GUI에 대한 사용자 입력이 처리되는 방식을 지정하는 데 많은 결정권을 가집니다.
GUI에 두 가지 유형의 컨트롤이 항상 함께 표시되는 상황이 있을 수 있습니다. 예를 들어 몇 개의 가로 슬라이더가 있는 캐릭터 생성 화면을 만들 수 있습니다. 이러한 모든 슬라이더에는 플레이어가 조정 중인 요소를 파악할 수 있는 레이블이 필요합니다. 이 경우 모든 GUI.Label() 호출을 GUI.HorizontalSlider() 호출과 연결하거나 레이블과 슬라이더를 함께 포함하는 복합 컨트롤 을 만들 수 있습니다.
// 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() 를 호출하고 올바른 인자를 전달하면 가로 슬라이더와 페어링된 레이블이 제공됩니다. 복합 컨트롤을 작성할 때는 함수의 끝에 올바른 값을 반환하여 상호 작용하도록 해야 합니다.
정적 함수를 사용하면 독립적인 복합 컨트롤의 전체 컬렉션을 생성할 수 있습니다. 이렇게 하면 사용하려는 것과 동일한 스크립트에서 함수를 선언할 필요가 없습니다.
// 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 슬라이더를 생성할 것이지만 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;
}
}