[UNITY] Inspector Readonly

spixychz·2025년 11월 9일

Unity

목록 보기
12/14

커스텀 Attribute를 통해 Unity 인스펙터에서 특정 필드를 읽기 전용으로 표시하기 위한 기능

InspectorReadOnlyAttribute

using UnityEngine;

public class InspectorReadOnlyAttribute : PropertyAttribute { }
  • 필드에 [InspectorReadOnly] 붙이기 위해 존재
  • PropertyAttribute 상속

PropertyAttribute

Unity에서 필드의 인스펙터 표시 방식을 제어하기 위한 기본 클래스

  • ex) [Range(min, max)], [Tooltip(text)], [Header(text)], [Space(height)] ...
  • 실제 렌더링은 UnityEditor의 PropertyDrawer가 담당
  • 작동 구조
    • 필드에 Attribuate 부착
    • 에디터가 SerializedProperty를 그릴 때 Attribute 감지
    • 일치하는 PropertyDrawer가 표시
  • 여러 Attribute를 함께 붙일 수는 있지만, PropertyDrawer는 필드당 하나만 적용

InspectorReadOnlyDrawer

using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(InspectorReadOnlyAttribute))]
public class InspectorReadOnlyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        GUI.enabled = false;
        EditorGUI.PropertyField(position, property, label, true);
        GUI.enabled = true;
    }
}
#endif
  • CustomPropertyDrawer를 통해 InspectorReadOnlyAttribute가 붙은 필드에만 작동
  • OnGUI 내부에서 GUI.enabled = false로 설정하여 입력을 막고, 이후 복원

ETC

Unity의 Attribute

  • 직렬화 관련 (Serialization)
    • 인스펙터 표시, 저장 여부
    • [SerializeField], [NonSerialized], [System.Serializable], [HideInInspector]
  • 인스펙터 표시 제어 (Property)
    • 인스펙터 UI 제어
    • [Range], [Tooltip], [Header], [Space], [Multiline]
  • 실행 제어 (Execution)
    • 실행 시점 및 에디터 동작
    • [ExecuteInEditMode], [ContextMenu]
  • 코드/컴포넌트 제약
    • 컴포넌트 구조와 스크립트 메타 설정
    • [RequireComponent], [AddComponentMenu]
profile
UNITY로 게임 개발하는 사람

0개의 댓글