PropertyDrawer

作者:追风剑情 发布于:2017-12-25 17:32 分类:Unity3d

工程截图

3333.png

示例代码

Range1PropertyDrawer.cs


using UnityEngine;
using UnityEditor;
using System.Collections;

//自定义特性属性样式
[CustomPropertyDrawer(typeof(Range1Attribute))]
public class Range1PropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //获得字段特性
        Range1Attribute range = attribute as Range1Attribute;

        //根据字段类型绘制不同的GUI
        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(position, property, range.min, range.max, label);
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label);
        else
            EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
    }
}


Test.cs


using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    [Range1(0.0F, 10.0F)]
    public float myFloat = 0.0F;
}


效果

1111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号