ClampDrawer.cs 713 B

123456789101112131415161718192021
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons.Editor
  4. {
  5. [CustomPropertyDrawer(typeof(ClampAttribute))]
  6. public class ClampAttributeDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. EditorGUI.PropertyField(position, property);
  11. Vector2 clampLimits = ((ClampAttribute)attribute).ClampLimits;
  12. if(property.propertyType == SerializedPropertyType.Float)
  13. property.floatValue = Mathf.Clamp(property.floatValue, clampLimits.x, clampLimits.y);
  14. else if(property.propertyType == SerializedPropertyType.Integer)
  15. property.intValue = Mathf.Clamp(property.intValue, (int)clampLimits.x, (int)clampLimits.y);
  16. }
  17. }
  18. }