dbrizov / NaughtyAttributes

Attribute Extensions for Unity
MIT License
4.53k stars 464 forks source link

[Request] MinMaxValue to support dynamic variables #235

Open TheEmbracedOne opened 3 years ago

TheEmbracedOne commented 3 years ago

It would be really handy if one could specify min/max values from other existing variables, like with ProgressBars:

public class NaughtyComponent : MonoBehaviour {
    [MinValue("minStamina"), MaxValue("maxStamina)"]
    public int myInt;

    public int minStamina;
    public int maxStamina;
}

public class NaughtyComponent : MonoBehaviour {
    [MinValue("minValue"), MaxValue("maxValue)"]
    public float myFloat;

    public float minValue;
    public float maxValue;
}

This would reduce the checks one would need to do to make sure values remain within ranges that could dynamically change. Thank you

TylerTemp commented 8 months ago

As this project hasn't merge PR for years, here is one and it is compatible to and can work together with NaughtyAttributes

I'll just paste the example:

public class MinMaxSliderExample: MonoBehaviour
{
    [field: SerializeField, MinMaxSlider(-100f, 100f)]
    public Vector2 OuterRange { get; private set; }

    [SerializeField, MinMaxSlider(nameof(GetOuterMin), nameof(GetOuterMax), 1)] public Vector2Int _innerRange;

    private float GetOuterMin() => OuterRange.x;
    private float GetOuterMax() => OuterRange.y;

    [field: SerializeField]
    public float DynamicMin { get; private set; }
    [field: SerializeField]
    public float DynamicMax { get; private set; }

    [SerializeField, MinMaxSlider(nameof(DynamicMin), nameof(DynamicMax))] private Vector2 _propRange;
    [SerializeField, MinMaxSlider(nameof(DynamicMin), 100f)] private Vector2 _propLeftRange;
    [SerializeField, MinMaxSlider(-100f, nameof(DynamicMax))] private Vector2 _propRightRange;
}

minmaxslider