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 에디터에서 인스펙터를 보다 사용하기 쉽고 시각적으로 매력적으로 만들어줍니다. 이를 통해 개발자는 더 나은 사용자 경험을 제공할 수 있으며, 다양한 방법으로 데이터를 효율적으로 제시할 수 있습니다.