Unity에서 IMGUI로 사용자 인터페이스(UI) 생성하기
이 문서는 Unity의 Immediate Mode GUI (IMGUI)를 사용하여 사용자 인터페이스를 생성하는 방법에 대해 설명합니다. 기본적으로 IMGUI는 Unity에서 실시간으로 UI를 만들고 조작할 수 있는 기능을 제공하며, 다양한 형태의 컨트롤을 구현할 수 있습니다.
1. Label (레이블)
레이블은 사용자가 클릭할 수 없는 비상호작용적 컨트롤입니다. 정보를 표시하는 데 사용됩니다.
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
GUI.Label (new Rect (25, 25, 100, 30), "Label");
}
}
2. Button (버튼)
버튼은 클릭 가능한 인터페이스 요소로, 버튼을 클릭하면 해당 코드가 실행됩니다.
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
if (GUI.Button (new Rect (25, 25, 100, 30), "Button"))
{
// 버튼 클릭 시 실행할 코드
}
}
}
3. Repeat Button (반복 버튼)
반복 버튼은 마우스 버튼을 눌러놓는 동안 매 프레임마다 반응하는 버튼입니다.
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
void OnGUI ()
{
if (GUI.RepeatButton (new Rect (25, 25, 100, 30), "RepeatButton"))
{
// 반복 버튼을 누르고 있는 동안 실행할 코드
}
}
}
4. TextField (텍스트 필드)
텍스트 필드는 한 줄의 문자열을 입력할 수 있는 필드입니다.
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);
}
}
5. TextArea (텍스트 영역)
텍스트 영역은 여러 줄로 입력할 수 있는 편집 가능한 영역입니다.
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);
}
}
6. Toggle (토글)
토글은 켜기/끄기 상태를 전환하는 체크박스입니다.
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");
}
}
7. Toolbar (툴바)
툴바는 여러 버튼을 가지며, 하나의 버튼만 활성화될 수 있는 행입니다.
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);
}
}
8. SelectionGrid (선택 그리드)
선택 그리드는 여러 버튼을 그리드 형식으로 배열하여 하나의 버튼을 선택할 수 있게 합니다.
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);
}
}
9. HorizontalSlider (수평 슬라이더)
수평 슬라이더는 값의 최소값과 최대값 사이를 드래그로 변경할 수 있습니다.
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);
}
}
10. ScrollView (스크롤 뷰)
스크롤 뷰는 큰 컨트롤 집합을 볼 수 있는 영역을 표시하는 데 사용됩니다.
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{
private Vector2 scrollViewVector = Vector2.zero;
private string innerText = "I am inside the ScrollView";
void OnGUI ()
{
scrollViewVector = GUI.BeginScrollView (new Rect (25, 25, 100, 100), scrollViewVector, new Rect (0, 0, 400, 400));
innerText = GUI.TextArea (new Rect (0, 0, 400, 400), innerText);
GUI.EndScrollView();
}
}
이 문서에서는 Unity의 IMGUI를 사용하여 다양한 UI 컨트롤을 생성하는 방법을 살펴보았습니다. 각 예제 코드를 참고하여 게임 또는 앱에서 필요한 UI를 손쉽게 구현할 수 있습니다.