1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| using System.Collections; using System.Collections.Generic; using System; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public sealed class RangeIntAttribution : PropertyAttribute { public readonly float minValue; public readonly float maxValue;
public RangeIntAttribution(float minValue, float maxValue) { this.minValue = minValue; this.maxValue = maxValue; } }
#if UNITY_EDITOR [CustomPropertyDrawer(typeof(RangeIntAttribution))] public sealed class RangeIntDrawer:PropertyDrawer { public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return 90; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
RangeIntAttribution attribute = this.attribute as RangeIntAttribution; property.floatValue = Mathf.Clamp(property.floatValue, attribute.minValue, attribute.maxValue); EditorGUI.HelpBox(new Rect(position.x,position.y,position.width,35), string.Format( "请输入一个范围从{0}到{1}的整数",attribute.minValue, attribute.maxValue), MessageType.Info); EditorGUI.PropertyField(new Rect(position.x, position.y + 35, position.width, 20), property, label);
if(property.propertyType == SerializedPropertyType.Float) { EditorGUI.Slider(new Rect(position.x, position.y + 65, position.width, 20), property,attribute.minValue, attribute.maxValue); } else if (property.propertyType == SerializedPropertyType.Integer) { EditorGUI.IntSlider(new Rect(position.x, position.y + 65, position.width, 20), property, Convert.ToInt32(attribute.minValue), Convert.ToInt32(attribute.maxValue)); } else { EditorGUI.LabelField(new Rect(position.x, position.y + 65, position.width, 20),label.text, "use Range with int or float"); } } } #endif
|