Search code examples
unity-game-engine

How to set an Inspector Attribute like a bar with min and max values?


I know there's the [Range] attribute so you can read one value between a min and a max.

But I'm looking to something to represent an attribute like the Inner / Outer Spot Angle which allows you to use the same bar, but choosing 2 values that follow:

  • Same as [Range] (min and max)
  • Accepts 2 values: min and max
  • input value min has the constraint of being always less or equal than max

Example from the Spot Light 2D component:

MinMaxRange vs Range

Falloff Strength is rendered as a [Range] but I'm looking how to represent a Vector2 (or just a (float, float) tuple) as the upper one (if possible)


Solution

  • These are called "MinMaxSlider"s. Unity has it's own implementation of these for custom inspector layouts, EditorGUILayout.MinMaxSlider, but there's several open-source packages that add one as an attribute you can add to any serialized Vector2; so you don't have to create your own custom layout for every class you want to use it with.

    Just searching online I've found ayellowpaper's MinMaxSlider on GitHub, which you can directly add from a GitHub URL into the package manager (make sure to add .git to the end of the URL).

    using UnityEngine;
    using Zelude;
    
    public class TestClass : MonoBehaviour {
        [MinMaxSlider(-100, 100)]
        [SerializeField]
        private Vector2 minMaxVariable;
    }
    

    produces...

    Inspector image showing MinMaxSlider and its range