PropertyDrawer 自定义属性绘图

public class PlayerAttributeExample : MonoBehaviour
{
    //无滑块的属性
    public int VIPLevel = 0;

    //特性限定,有滑块
    [Range(0, 10)]
    public int SliderVIPLevel = 0;
}

Range特性的方法实现:

using UnityEngine;
using System.Collections;

//特性的定义要继承自PropertyAttribute
public class MyRangeAttribute : PropertyAttribute
{
    public float Min;//最小值
    public float Max;//最大值

    public MyRangeAttribute(float min, float max)
    {
        this.Min = min;
        this.Max = max;
    }
}
using UnityEngine;
using System.Collections;
using UnityEditor;

//继承PropertyDrawer, 必须放入Editor文件夹下
[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class MyRangeAttributeDrawer : PropertyDrawer
{
    //重载OnGUI方法
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MyRangeAttribute myRange = attribute as MyRangeAttribute;

        if (property.propertyType == SerializedPropertyType.Integer)
        {
            EditorGUI.IntSlider(position, property, (int)myRange.Min, (int)myRange.Max, label);
        }
        else if (property.propertyType == SerializedPropertyType.Float)
        {
            EditorGUI.Slider(position, property, myRange.Min, myRange.Max, label);
        }
        else
        {

        }
    }

}

2.绘制多选


 public enum SomeFood
    {
        汉堡 = 0,
        鸡肉卷 = 1,
        薯条 = 3,
    }
    //只能单选
    public SomeFood MyLoveFood;

    //多选特性
    [EnumListAttribute]
    public SomeFood MyLoveFoodList;
using UnityEngine;
using System.Collections;

public class EnumListAttribute : PropertyAttribute
{

}
using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomPropertyDrawer(typeof(EnumListAttribute))]
public class EnumListAttributeDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,822评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 昨日说德语从句中递归算法,实际上正如赫拉利所言,从计算机角度来看我认为这是很容易进行抽象理解的,不烧脑也不骇俗:一...
    掌门_艾老师阅读 291评论 0 1
  • 虽然都说一个人的时光是最美好的,是能够让你变得优秀的必经之路,但是我无法脱离群体去独处,而最大的原因是因为害怕。 ...
    拾梦_阅读 337评论 0 0
  • 很好看,Baymax很可愛,許多有趣的點。不過,就只是很好看,我喜歡,但欠缺了一些能夠讓人神迷的元素。 觀影前沒做...
    陽冰阅读 304评论 0 1