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.
You need a Range Attribute.
using UnityEngine;
public class Example : MonoBehaviour
{
// This integer will be shown as a slider,
// with the range of 1 to 6 in the Inspector
[Range(1, 6)]
public int integerRange;
// This float will be shown as a slider,
// with the range of 0.2f to 0.8f in the Inspector
[Range(0.2f, 0.8f)]
public float floatRange;
}
Range Attribute only give it a slider, I hope there’s also 2 labels for minimum and maximum value.
Yes it’s possible, you would just have to write a property drawer for it.
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.
You mean this?
https://docs.unity3d.com/ScriptReference/EditorGUILayout.MinMaxSlider.html
There is a UI Toolkit version of it too:
https://docs.unity3d.com/ScriptReference/UIElements.MinMaxSlider.html
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.