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?
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
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
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(LogarithmicRangeAttribute))]
public class LogarithmicRangeDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
LogarithmicRangeAttribute attribute = (LogarithmicRangeAttribute) this.attribute;
if (property.propertyType != SerializedPropertyType.Float) {
EditorGUI.LabelField(position, label.text, "Use LogarithmicRange with float.");
return;
}
Slider(position, property, attribute.min, attribute.max, attribute.power, label);
}
public static void Slider(
Rect position, SerializedProperty property,
float leftValue, float rightValue, float power, GUIContent label) {
label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginChangeCheck();
float num = PowerSlider(position, label, property.floatValue, leftValue, rightValue, power);
if (EditorGUI.EndChangeCheck())
property.floatValue = num;
EditorGUI.EndProperty();
}
public static float PowerSlider(Rect position, GUIContent label, float value, float leftValue, float rightValue, float power) {
var editorGuiType = typeof(EditorGUI);
var methodInfo = editorGuiType.GetMethod(
"PowerSlider",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static,
null,
new[] {typeof(Rect), typeof(GUIContent), typeof(float), typeof(float), typeof(float), typeof(float)},
null);
if (methodInfo != null) {
return (float)methodInfo.Invoke(null, new object[]{position, label, value, leftValue, rightValue, power});
}
return leftValue;
}
}
#endif
[AttributeUsage(AttributeTargets.Field)]
public class LogarithmicRangeAttribute : PropertyAttribute {
public readonly float min = 1e-3f;
public readonly float max = 1e3f;
public readonly float power = 2;
public LogarithmicRangeAttribute(float min, float max, float power) {
if (min <= 0) {
min = 1e-4f;
}
this.min = min;
this.max = max;
this.power = power;
}
}
1 Like
Was doing some audio related editor stuff and cooked one up myself. This one allows for a custom min, max and a value for the center.
1 Like