ReadOnly Property

김기훈·2025년 2월 10일
0

Unity

목록 보기
6/7

Custom Attribute

Unity 기본 제공 기능외에, 커스텀 속성(Attribute)을 만들어 사용할 수 있다.

ReadOnly

[ReadOnly] 속성을 변수에 적용하면 인스펙터 값이 표시되지만 수정할 수 없는 상태가 된다.

▪️코드

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class ReadOnlyAttribute : PropertyAttribute { }

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        GUI.enabled = false; // 수정 불가능하게 설정
        EditorGUI.PropertyField(position, property, label);
        GUI.enabled = true;  // 다시 활성화
    }
}
#endif
profile
Late Bloomer

0개의 댓글