using System;
using UnityEngine;
using UnityEditor;
namespace AiryUI
{
public static class EditorGUILayoutExtensions
{
public static void ToggleLeft(this SerializedProperty prop, GUIContent label, params GUILayoutOption[] options)
{
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = prop.hasMultipleDifferentValues;
var newValue = EditorGUILayout.ToggleLeft(label, prop.boolValue, options);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
prop.boolValue = newValue;
}
///
/// Create a EditorGUILayout.Slider which properly handles multi-object editing
/// We apply the 'convIn' conversion to the SerializedProperty value before exposing it as a Slider.
/// We apply the 'convOut' conversion to the Slider value to store it back to the SerializedProperty.
///
/// The value the slider shows. This determines the position of the draggable thumb.
/// Label in front of the slider.
/// The value at the left end of the slider.
/// The value at the right end of the slider.
/// Conversion function applied on the SerializedProperty to get the Slider value
/// Conversion function applied on the Slider value to get the SerializedProperty
public static void FloatSlider(
this SerializedProperty prop,
GUIContent label,
float leftValue, float rightValue,
Func convIn,
Func convOut,
params GUILayoutOption[] options)
{
var floatValue = convIn(prop.floatValue);
EditorGUI.BeginChangeCheck();
{
EditorGUI.showMixedValue = prop.hasMultipleDifferentValues;
{
floatValue = EditorGUILayout.Slider(label, floatValue, leftValue, rightValue, options);
}
EditorGUI.showMixedValue = false;
}
if (EditorGUI.EndChangeCheck())
prop.floatValue = convOut(floatValue);
}
}
}