miepzerino
2025-04-03 4f1ed3919b0ee3f89dbcbacf49990888a7d9274a
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
using System;
using System.Reflection;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
 
namespace Assets.Scripts.Attributes
{
    // Custom attribute for the min-max slider
    public class MinMaxAttribute : PropertyAttribute
    {
        public float Min { get; private set; }
        public float Max { get; private set; }
 
        public MinMaxAttribute(float min, float max)
        {
            Min = min;
            Max = max;
        }
    }
#if UNITY_EDITOR
    [CustomPropertyDrawer(typeof(MinMaxAttribute))]
    public class MinMaxDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.Vector2)
            {
                EditorGUI.LabelField(position, label.text, "Use MinMax with Vector2");
                return;
            }
 
            var minMaxAttribute = (MinMaxAttribute)attribute;
            var vector = property.vector2Value;
 
            EditorGUI.BeginProperty(position, label, property);
 
            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
 
            float[] values = new float[] { vector.x, vector.y };
            EditorGUI.MultiFloatField(position,
                new GUIContent[] { new GUIContent("Min"), new GUIContent("Max") },
                values);
 
            values[0] = Mathf.Clamp(values[0], minMaxAttribute.Min, values[1]);
            values[1] = Mathf.Clamp(values[1], values[0], minMaxAttribute.Max);
 
            property.vector2Value = new Vector2(values[0], values[1]);
 
            EditorGUI.EndProperty();
        }
    }
#endif
}