Unity UI Toolkit 예제 가이드

이 문서는 Unity의 UI Toolkit을 이용하여 사용자 인터페이스(UI)를 만드는 방법을 설명합니다. 아래의 예제를 통해 상대 및 절대 위치 지정을 사용하는 방법을 배울 수 있습니다.

개요

이 예제에서는 UI 컨트롤을 추가하고 스타일을 지정하는 방법을 설명합니다. 자동 레이아웃 시스템을 활용하여 에디터와 런타임 UI에 요소를 추가합니다.

  • 상대 포지션: 특정 기준을 가지고 요소를 배치합니다.
  • 절대 포지션: 화면의 절대 위치에 요소를 배치합니다.

선행 조건

이 가이드는 Unity 에디터와 C# 스크립팅에 익숙한 개발자를 위한 것입니다. 다음 사항을 먼저 숙지해야 합니다.

  • 시각적 트리
  • 좌표 및 포지션 시스템
  • UXML 및 USS

에디터 UI 예제 만들기

  1. Unity 프로젝트를 생성합니다.
  2. Project 창을 오른쪽 클릭하고 Create > UI Toolkit > Editor Window를 선택합니다.
  3. 새로운 창의 이름을 PositioningTestWindow로 설정하고 확인합니다.
  4. 다음 코드를 PositioningTestWindow.cs 파일에 입력합니다.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class PositioningTestWindow : EditorWindow
{
    [MenuItem("Window/UI Toolkit/Positioning Test Window")]
    public static void ShowExample()
    {
        var wnd = GetWindow<PositioningTestWindow>();
        wnd.titleContent = new GUIContent("Positioning Test Window");
    }

    public void CreateGUI()
    {
        for (int i = 0; i < 2; i++)
        {
            var temp = new VisualElement();
            temp.style.width = 70;
            temp.style.height = 70;
            temp.style.marginBottom = 2;
            temp.style.backgroundColor = Color.gray;
            this.rootVisualElement.Add(temp);
        }

        // Relative positioning
        var relative = new Label("Relative\nPos\n25, 0");
        relative.style.width = 70;
        relative.style.height = 70;
        relative.style.left = 25;
        relative.style.marginBottom = 2;
        relative.style.backgroundColor = new Color(0.2165094f, 0, 0.254717f);
        this.rootVisualElement.Add(relative);

        for (int i = 0; i < 2; i++)
        {
            var temp = new VisualElement();
            temp.style.width = 70;
            temp.style.height = 70;
            temp.style.marginBottom = 2;
            temp.style.backgroundColor = Color.gray;
            this.rootVisualElement.Add(temp);
        }

        // Absolute positioning
        var absolutePositionElement = new Label("Absolute\nPos\n25, 25");
        absolutePositionElement.style.position = Position.Absolute;
        absolutePositionElement.style.top = 25;
        absolutePositionElement.style.left = 25;
        absolutePositionElement.style.width = 70;
        absolutePositionElement.style.height = 70;
        absolutePositionElement.style.backgroundColor = Color.black;
        this.rootVisualElement.Add(absolutePositionElement);
    }
}
  1. 메뉴에서 Window > UI Toolkit > Positioning Test Window를 선택하면 결과를 확인할 수 있습니다.

런타임 UI 예제 만들기

  1. PositioningTest.uss라는 이름의 USS 파일을 생성합니다.
.box {
    height: 70px;
    width: 70px;
    margin-bottom: 2px;
    background-color: gray;
}
#relative {
    width: 70px; 
    height: 70px; 
    background-color: purple; 
    left: 25px; 
    margin-bottom: 2px;
    position: relative;
}
#absolutePositionElement {
    left: 25px; 
    top: 25px; 
    width: 70px; 
    height: 70px; 
    background-color: black;
    position: absolute;
}
  1. PositioningTest.uxml라는 이름의 UXML 문서를 생성합니다.
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" 
xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" 
editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd" 
editor-extension-mode="False">
    <Style src="PositioningTest.uss"/>
    <ui:VisualElement class="box"/>
    <ui:VisualElement class="box"/>
    <ui:Label text="Relative\nPos\n25, 0" name="relative" />
    <ui:VisualElement class="box"/>
    <ui:VisualElement class="box"/>
    <ui:Label text="Absolute\nPos\n25, 25" name="absolutePositionElement" />  
</ui:UXML>
  1. PositioningTestRuntime.cs라는 이름의 C# 스크립트를 생성합니다.
using UnityEngine;
using UnityEngine.UIElements;

public class PostionTestRuntime : MonoBehaviour
{
    void OnEnable()
    {
        GetComponent<UIDocument>();
    }
}

결과 확인

  1. 계층(Hierarchy) 창을 오른쪽 클릭한 후 UI Toolkit > UI Document를 선택합니다.
  2. UI Document의 인스펙터 창에서 UI Document > Source Asset > PositioningTest를 선택합니다.
  3. UI Document의 인스펙터 창에서 Add Component > Positioning Test Runtime을 선택합니다.
  4. 플레이 모드로 들어가 결과를 확인합니다.

추가 리소스

이 가이드를 통해 Unity의 UI Toolkit을 활용하여 다양한 UI 요소를 쉽게 배치하고 스타일링 할 수 있는 방법을 배우셨기를 바랍니다.

Read more

Unity 매뉴얼 스크립팅 API 해설

이 문서는 Unity의 매뉴얼 스크립팅 API에 대한 간단한 해설과 활용 예제들을 포함하고 있습니다. Unity는 게임 개발 플랫폼으로, 스크립팅 API를 통해 게임의 다양한 기능을 제어하고 수정할 수 있습니다. 버전 Unity 스크립팅 API는 여러 버전으로 제공됩니다. 주의 깊게 선택하여 사용하는 것이 중요합니다. 버전 설명 2023.2 최신 기능 및 버그 수정이 추가됨

By 이재협/실장/시스템개발실/PHYSIA

Unity 매뉴얼 스크립팅 API 설명서 해설

이 문서는 Unity의 매뉴얼 스크립팅 API에 대한 정보를 제공하며, 버전에 따라 다르게 적용되는 내용들을 설명합니다. 본 문서에서는 주요 내용을 간단히 정리하고 활용 가능 예제를 통해 이해를 돕겠습니다. 기본 개념 Unity에서 스크립팅 API는 게임 오브젝트와 그들의 동작을 제어하기 위한 강력한 도구입니다. 이를 통해 게임의 로직, 물리 엔진, 애니메이션 및 사용자 인터페이스를

By 이재협/실장/시스템개발실/PHYSIA

Unity 스크립팅 API 가이드

이 문서는 Unity의 스크립팅 API에 대해 설명합니다. Unity는 게임 개발을 위한 인기 있는 엔진으로, 강력한 스크립팅 기능을 제공합니다. 이 가이드는 Unity에서 스크립트를 작성하고 사용하는 방법을 이해하는 데 도움을 드립니다. 목차 * Unity 스크립팅 소개 * 기본 스크립트 생성 * 스크립트 사용 예제 * 응용 프로그램 * 참고 자료 Unity 스크립팅 소개 Unity는 C# 프로그래밍 언어를

By 이재협/실장/시스템개발실/PHYSIA