Unity 커스텀 프로퍼티 드로어 사용 방법

이 문서는 Unity의 커스텀 프로퍼티 드로어에 대한 설명과 활용 예제를 제공합니다. 커스텀 프로퍼티 드로어는 인스펙터에서 특정 클래스의 속성을 시각적으로 제어하고 개선하는 데 사용됩니다.

커스텀 프로퍼티 드로어란?

커스텀 프로퍼티 드로어는 Unity 인스펙터에서 특정 스크립트의 속성이 어떻게 나타나는지를 커스터마이즈하는 데 사용됩니다. 이를 통해 사용자에게 더 나은 사용 경험을 제공할 수 있습니다.

사용 이유

  • Serializable 클래스의 GUI 커스터마이즈
  • 스크립트 멤버의 GUI 최적화

예제: Ingredient 클래스

아래는 간단한 Ingredient 클래스를 정의한 예제입니다. 이 클래스는 재료의 이름, 양, 그리고 단위를 포함합니다.

using System;
using UnityEngine;

enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
[Serializable]
public class Ingredient
{
    public string name;
    public int amount = 1;
    public IngredientUnit unit;
}

public class Recipe : MonoBehaviour
{
    public Ingredient potionResult;
    public Ingredient[] potionIngredients;
}

이 클래스를 인스펙터에서 어떻게 커스터마이즈 할 수 있는지 알아보겠습니다.

커스텀 프로퍼티 드로어 구현

아래는 Ingredient 클래스의 인스펙터 표현을 커스터마이즈 하는 IngredientDrawer 클래스입니다.

using UnityEditor;
using UnityEngine;

// IngredientDrawer
[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        var amountRect = new Rect(position.x, position.y, 30, position.height);
        var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);

        EditorGUI.indentLevel = indent;
        EditorGUI.EndProperty();
    }
}

다른 속성 속성과의 통합

커스텀 속성을 만들어 인스펙터에 다양한 형태로 표시할 수 있습니다. 예를 들어, RangeAttribute를 사용하여 슬라이더로 값을 조절할 수 있습니다.

아래는 사용자가 정의한 MyRangeAttribute와 그에 맞는 프로퍼티 드로어 구현 예제입니다.

사용자 정의 범위 속성

using UnityEngine;

public class MyRangeAttribute : PropertyAttribute 
{
    public readonly float min;
    public readonly float max;

    public MyRangeAttribute(float min, float max)
    {
        this.min = min;
        this.max = max;
    }
}

사용자 정의 범위 드로어

using UnityEditor;
using UnityEngine;

// Tell the MyRangeDrawer that it is a drawer for properties with the MyRangeAttribute.
[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
    void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MyRangeAttribute range = (MyRangeAttribute)attribute;

        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(position, property, range.min, range.max, label);
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(position, property, (int) range.min, (int) range.max, label);
        else
            EditorGUI.LabelField(position, label.text, "Use MyRange with float or int.");
    }
}

결론

커스텀 프로퍼티 드로어는 Unity 에디터에서 인스펙터를 보다 사용하기 쉽고 시각적으로 매력적으로 만들어줍니다. 이를 통해 개발자는 더 나은 사용자 경험을 제공할 수 있으며, 다양한 방법으로 데이터를 효율적으로 제시할 수 있습니다.

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