baobao
1/31/2018 - 2:00 PM

カスタムAttributeテスト

カスタムAttributeテスト

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

/// <summary>
/// カスタムAttributeテスト(以下の2クラスが最低限必要)
/// ・PropertyAttributeを継承したAttributeクラス
/// ・PropertyDrawerを継承したDrawerクラス
/// </summary>
public class Test : MonoBehaviour
{
    [SerializeField]
    Piyo piyo;
}

[System.Serializable]
public class Piyo
{
    [SerializeField, SliderTest(-1, 5)]
    int hp;
    [SerializeField]
    string label;
}

class SliderTestAttribute : PropertyAttribute
{
    public readonly int min;
    public readonly  int max;

    public SliderTestAttribute(int min, int max)
    {
        this.min = min;
        this.max = max;
    }
}


#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SliderTestAttribute))]
class Int2SliderDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, 
                               SerializedProperty property, GUIContent label)
    {
        SliderTestAttribute attr = (SliderTestAttribute)attribute;

        if (property.propertyType == SerializedPropertyType.Integer)
        {
            EditorGUI.IntSlider(position, property, attr.min, attr.max);
        }

    }
}
#endif