Search Unity

Question Float value with a slider and 2 labels for max and minimum value

Discussion in 'Scripting' started by renawevin, Feb 15, 2024.

  1. renawevin

    renawevin

    Joined:
    Dec 15, 2018
    Posts:
    4
    Is it possible to display a float value in inspector with a slider and 2 labels for maximum and minimum value?
    Just like the Match value of Canvas Scaler.

    upload_2024-2-15_15-43-28.png
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    You need a Range Attribute.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.     // This integer will be shown as a slider,
    6.     // with the range of 1 to 6 in the Inspector
    7.     [Range(1, 6)]
    8.     public int integerRange;
    9.  
    10.     // This float will be shown as a slider,
    11.     // with the range of 0.2f to 0.8f in the Inspector
    12.     [Range(0.2f, 0.8f)]
    13.     public float floatRange;
    14. }
     
  3. renawevin

    renawevin

    Joined:
    Dec 15, 2018
    Posts:
    4
    Range Attribute only give it a slider, I hope there's also 2 labels for minimum and maximum value.

    upload_2024-2-15_16-31-53.png
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,812
    Yes it's possible, you would just have to write a property drawer for it.
     
    CodeRonnie and Bunny83 like this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,979
    Even though it's quite rare, in this case using a DecoratorDrawer may be more suited. Though I barely ever used one and afaik the "decoration" is usually above the actual property which isn't really what the original question was about since the labels are below. So after all it might need to be a PropertyDrawer, but you could give it a shot. DecoratorDrawers are great as they can add stuff to properties without changing the actual drawing of the property. So they can be reused more freely. Like the "Header" or "Space" attribute. Those belong to DecoratorDrawers.

    PropertyDrawers have the problem, that only one can be applied to a field.
     
    CodeRonnie and spiney199 like this.
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    464
    CodeRonnie and Bunny83 like this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,979
    That's what I thought in the first place. Though he only talked about the labels for the min / max values of the slider.

    Funny enough, the Unity editor does have those labels for the Slider in an internal struct. So the slider has the ability to display labels, but this is not exposed it seems. The internal DoSlider method does actually use this information to show the labels for the min / max values. Since all of that is internal, you would need to use reflection to manually set those. It may be the easiest solution but is kinda ugly.
     
    CodeRonnie and Lurking-Ninja like this.