인스펙터 읽기 전용 만들기 (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 : [링크]
★★☆☆☆
반응형
댓글