다양한 IMGUI 컨트롤 을 생성할 수 있습니다. 이 섹션에는 사용 가능한 디스플레이 및 인터랙티브 컨트롤이 모두 나열되어 있습니다. 컨트롤의 레이아웃에 영향을 주는 다른 IMGUI 함수는 가이드의 레이아웃 섹션에 설명되어 있습니다.
Label 은 상호 작용할 수 없는 요소입니다. 표시용으로만 제공됩니다. 클릭하거나 이동할 수 없습니다. 정보를 표시하는 용도로만 사용하는 것이 가장 좋습니다.
/* GUI.Label example */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
GUI.Label (new Rect (25, 25, 100, 30), "Label");
}
}
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
}
}
}
RepeatButton 은 일반 Button 의 배리에이션입니다. 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 컨트롤은 텍스트 문자열을 포함하는 단일 행의 상호 작용 필드입니다.
/* 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);
}
}
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);
}
}
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");
}
}
Toolbar 컨트롤은 기본적으로 Button 으로 이뤄진 행입니다. 툴바의 버튼은 한 번에 하나만 활성화할 수 있으며 다른 버튼을 클릭할 때까지 활성 상태로 유지됩니다. 이 동작은 일반 툴바 동작을 에뮬레이트합니다. 툴바에 임의의 버튼 수를 정의할 수 있습니다.
/* 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 컨트롤은 행이 여러 개인 Toolbar 입니다. 그리드에 있는 열과 행의 수를 결정할 수 있습니다. 한 번에 하나의 버튼만 활성화할 수 있습니다.
/* 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);
}
}
HorizontalSlider 컨트롤은 드래그하여 사전 설정된 최솟값과 최댓값 사이의 값으로 변경할 수 있는 일반적인 가로 슬라이딩 노브입니다.
/* 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 컨트롤은 드래그하여 사전 설정된 최솟값과 최댓값 사이의 값으로 변경할 수 있는 일반적인 세로 슬라이딩 노브입니다.
/* 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 컨트롤은 Slider 컨트롤과 비슷하지만 시각적으로 웹 브라우저 또는 워드 프로세서의 스크롤 요소와 유사합니다. 이 컨트롤은 ScrollView 컨트롤을 탐색하는 데 사용됩니다.
/* 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 컨트롤과 비슷하지만 시각적으로 웹 브라우저 또는 워드 프로세서의 스크롤 요소와 유사합니다. 이 컨트롤은 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 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();
}
}
Window 는 드래그 가능한 컨트롤 컨테이너입니다. 클릭 시 포커스를 받거나 잃을 수 있습니다. 따라서 다른 컨트롤과는 약간 다르게 구현됩니다. 각 Window에는 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.changed 값을 읽습니다. 이렇게 하면 사용자가 어떤 작업을 수행했을 때 true로 설정되어 사용자 입력을 쉽게 확인할 수 있습니다.
일반적인 시나리오는 툴바에서 클릭한 버튼에 따라 특정 값을 변경하고자 할 때입니다. 이 경우 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 값을 반환합니다.