본문 바로가기
개발/Unity

Unity) 인스펙터 읽기 전용 만들기 (Inspector ReadOnly Property)

by 테샤르 2020. 10. 15.

인스펙터 읽기 전용 만들기 (Inspecator ReadOnly PropertyInspector ReadOnly Property)

유니티 개발을 하다보면. Property를 노출하는 과정에서 변경이 안되어야 하는 상황이 생긴다.

ReadOnly 속성으로 읽기만 가능하도록 변경을 해서 사용해야하는 경우에서 사용 가능하다.

 

< 적용 전  / 적용 후  >

기능은 심플하다. 노출은 되지만 수정이 불가능한 상태로 돼서 '변경되지 않도록' 값을 쓰지 못하게 만드는 것이다.

반응형

 

코드는 다음과 같다.

< 코드 >

PropertyDrawer을 이용한 GUI.enable을 사용한 방법이다.

using UnityEngine;
using UnityEditor;

public class ReadOnlyAttribute : PropertyAttribute
{

}

[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property,
                                            GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property, label, true);
    }

    public override void OnGUI(Rect position,
                               SerializedProperty property,
                               GUIContent label)
    {
        GUI.enabled = false;
        EditorGUI.PropertyField(position, property, label, true);
        GUI.enabled = true;
    }
}

 

사용하는 곳은 다음과 같다. [ReadOnlyAttribute]로 프로퍼티를 명명한다.

 	
    [SerializeField]
 #if UNITY_EDITOR
    [ReadOnlyAttribute]
#endif
    private string userAccountID= "";

 

Unity Attribute : [링크]

 

속성 - Unity 매뉴얼

Attributes 는 스크립트에서 클래스, 프로퍼티 또는 함수 위에 명시하여 특별한 동작을 나타낼 수 있는 마커입니다. 예를 들어, 프로퍼티 선언 위에 HideInInspector 속성을 추가하여 인스펙터가 공용

docs.unity3d.com

 

 ★

 

반응형

댓글