Search Unity

Logarithmic slider?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by virror, Jan 12, 2015.

  1. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    Any thoughts about adding the possibility to make a slider logarithmic? This would be very useful for many cases. If i want to do it myself i guess i have to make a completely new Slider component?
     
  2. Kavukamari

    Kavukamari

    Joined:
    Jan 2, 2016
    Posts:
    5
    I was looking for the same thing.

    It's been a while, but maybe this will help: I just thought to take the value in as a linear value, and then apply the exponential/logarithm in the script itself

    maybe you can somehow override the readout to say the post-modified value as well, but the "actual" value of the slider is the linear one, still
     
  3. pld

    pld

    Joined:
    Dec 12, 2014
    Posts:
    7
    I know this is an ancient thread, but Unity's already implemented a power/log slider; and regular sliders are simply power sliders with a power of 1.0f. However, the dang method is internal :-/

    Try this

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [CustomPropertyDrawer(typeof(LogarithmicRangeAttribute))]
    3. public class LogarithmicRangeDrawer : PropertyDrawer {
    4.   public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    5.     LogarithmicRangeAttribute attribute = (LogarithmicRangeAttribute) this.attribute;
    6.     if (property.propertyType != SerializedPropertyType.Float) {
    7.       EditorGUI.LabelField(position, label.text, "Use LogarithmicRange with float.");
    8.       return;
    9.     }
    10.  
    11.     Slider(position, property, attribute.min, attribute.max, attribute.power, label);
    12.   }
    13.  
    14.   public static void Slider(
    15.       Rect position, SerializedProperty property,
    16.       float leftValue, float rightValue, float power, GUIContent label) {
    17.     label = EditorGUI.BeginProperty(position, label, property);
    18.     EditorGUI.BeginChangeCheck();
    19.     float num = PowerSlider(position, label, property.floatValue, leftValue, rightValue, power);
    20.  
    21.     if (EditorGUI.EndChangeCheck())
    22.       property.floatValue = num;
    23.     EditorGUI.EndProperty();
    24.   }
    25.  
    26.   public static float PowerSlider(Rect position, GUIContent label, float value, float leftValue, float rightValue, float power) {
    27.     var editorGuiType = typeof(EditorGUI);
    28.     var methodInfo = editorGuiType.GetMethod(
    29.         "PowerSlider",
    30.         System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static,
    31.         null,
    32.         new[] {typeof(Rect), typeof(GUIContent), typeof(float), typeof(float), typeof(float), typeof(float)},
    33.         null);
    34.     if (methodInfo != null) {
    35.       return (float)methodInfo.Invoke(null, new object[]{position, label, value, leftValue, rightValue, power});
    36.     }
    37.     return leftValue;
    38.   }
    39. }
    40. #endif
    41.  
    42.  
    43. [AttributeUsage(AttributeTargets.Field)]
    44. public class LogarithmicRangeAttribute : PropertyAttribute {
    45.   public readonly float min = 1e-3f;
    46.   public readonly float max = 1e3f;
    47.   public readonly float power = 2;
    48.   public LogarithmicRangeAttribute(float min, float max, float power) {
    49.     if (min <= 0) {
    50.       min = 1e-4f;
    51.     }
    52.     this.min = min;
    53.     this.max = max;
    54.     this.power = power;
    55.   }
    56. }
    57.