Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug RangeAttribute slider broken for negative values

Discussion in 'UI Toolkit' started by morriscurtis, Mar 6, 2023.

  1. morriscurtis

    morriscurtis

    Joined:
    Dec 13, 2014
    Posts:
    3
    Hi!

    I think the Unity Editor switched to using only UI Toolkit for it's inspector a view releases ago (2022.3f1 I believe, not sure). In any case: Since then, RangeSliders that involve a negative number are broken.

    Just slap this MonoBehaviour onto a GameObject and observe that the 3 sliders have broken initial states. If you click on them they will work correctly UNTIL you click away and back on the GameObject, which will break it again.

    Code (CSharp):
    1. using UnityEngine;
    2. public class RangeSliderTest : MonoBehaviour
    3. {
    4.     [SerializeField, Range(-1f, 0f)] private float rangeMinus1To0;
    5.     [SerializeField, Range(0f, 1f)] private float range0To1;
    6.     [SerializeField, Range(-1f, 1f)] private float rangeMinus1To1;
    7.     [SerializeField, Range(0, 100f)] private float range0To100;
    8.     [SerializeField, Range(-100f, 100f)] private float rangeMinus100To100;
    9. }
    RangeSliderTest.png
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
  3. morriscurtis

    morriscurtis

    Joined:
    Dec 13, 2014
    Posts:
    3
    Thanks for the reply! I reported it (IN-34646)
     
  4. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    5 months later and still broken in 2022.3.0f1.
    Here's a script that provides a replacement for anyone that needs it.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6.  
    7. public class MRangeAttribute : PropertyAttribute
    8. {
    9.     public float Min { get; }
    10.     public float Max { get; }
    11.  
    12.     public MRangeAttribute(float min, float max)
    13.     {
    14.         this.Min = min;
    15.         this.Max = max;
    16.     }
    17.  
    18. #if UNITY_EDITOR
    19.     [CustomPropertyDrawer(typeof(MRangeAttribute))]
    20.     public class Drawer : PropertyDrawer
    21.     {
    22.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    23.         {
    24.             return EditorGUIUtility.singleLineHeight;
    25.         }
    26.  
    27.         public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    28.         {
    29.             var attribute = base.attribute as MRangeAttribute;
    30.  
    31.             EditorGUI.BeginProperty(rect, label, property);
    32.  
    33.             switch (property.propertyType)
    34.             {
    35.                 case SerializedPropertyType.Float:
    36.                     property.floatValue = EditorGUI.Slider(rect, label, property.floatValue, attribute.Min, attribute.Max);
    37.                     break;
    38.  
    39.                 case SerializedPropertyType.Integer:
    40.                     property.floatValue = EditorGUI.IntSlider(rect, label, property.intValue, (int)attribute.Min, (int)attribute.Max);
    41.                     break;
    42.  
    43.                 default:
    44.                     EditorGUI.HelpBox(rect, $"Property ({property.name}) of type ({property.propertyType}) not Supported by MRange", MessageType.Error);
    45.                     break;
    46.             }
    47.  
    48.             EditorGUI.EndProperty();
    49.         }
    50.     }
    51. #endif
    52. }
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Its fixed in 2022.3.4f1
    https://issuetracker.unity3d.com/is...alue-when-the-range-contains-a-negative-value
     
    Moe_Baker and Noble-Woods like this.
  6. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    Thank you, just downloaded that version.
     
    karl_jones likes this.