工程截图
示例代码
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;
}
效果