unity 编辑器扩展 PropertyDrawer

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

public class MyTestAttribute : PropertyAttribute {

    public int max;
    public int min;
        
    public MyTestAttribute(int a, int b)
    {
        min = a;
        max = b;
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


/// <summary>
/// 扩展属性绘制器,用来扩展属性的查看内容和方式,用于debug非常方便
/// 官方编程手册的内容很不错,可以用来参考
/// </summary>
[CustomPropertyDrawer(typeof(MyTestAttribute))]
public class MyTestDrawer : PropertyDrawer {

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //base.OnGUI(position, property, label);

        MyTestAttribute attribute = (MyTestAttribute)base.attribute;
        property.intValue = Mathf.Min(Mathf.Max(EditorGUI.IntField(position, label.text, property.intValue), attribute.min), attribute.max);
    }
}

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

public class Game : MonoBehaviour {

    [MyTestAttribute(0, 100)]
    public int intValue = 0;
}

原文地址:https://www.cnblogs.com/yufenghou/p/7344284.html