Unity Editor 확장 Attribute

3899 단어 Unity

Attribute



편집은 할 수 없지만 표시되는 Attribute의 작성

ReadOnlyAttribute.cs를 만들고 아래 소스 작성

ReadOnlyAttribute
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//<summary>
//ReadOnlyというAttributeを定義するためのクラス
//クラス名に「Attribute」を入れる必要がある
//</summary>

#if UNITY_EDITOR
using UnityEditor;
#endif

public class ReadOnlyAttribute : PropertyAttribute
{

}

#if UNITY_EDITOR
[CustomPropertyDrawer (typeof (ReadOnlyAttribute))]
public class ReadOnlyAttributeDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginDisabledGroup(true);
        EditorGUI.PropertyField(position, property, label, true);
        EditorGUI.EndDisabledGroup();
    }
}
#endif

다른 스크립트에 다음 소스 작성

ExampleAttribute
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExampleAttribute : MonoBehaviour
{
    [ReadOnly]
    public int ReadOnlyPublicInt;

    [SerializeField, ReadOnly]
    private int ReadOnlyPrivateInt;
}

결과

좋은 웹페이지 즐겨찾기