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
|
}
|