com.unity.uiextensions.nosa.../Editor/FancyScrollView/ScrollerEditor.cs

119 lines
4.0 KiB
C#
Raw Normal View History

/// Credit setchi (https://github.com/setchi)
/// Sourced from - https://github.com/setchi/FancyScrollView
using UnityEditor;
using UnityEditor.AnimatedValues;
namespace UnityEngine.UI.Extensions
{
[CustomEditor(typeof(Scroller))]
[CanEditMultipleObjects]
public class ScrollerEditor : Editor
{
SerializedProperty viewport;
2019-11-16 22:49:21 +08:00
SerializedProperty scrollDirection;
SerializedProperty movementType;
SerializedProperty elasticity;
SerializedProperty scrollSensitivity;
SerializedProperty inertia;
SerializedProperty decelerationRate;
SerializedProperty snap;
2019-11-16 22:49:21 +08:00
SerializedProperty draggable;
SerializedProperty scrollbar;
AnimBool showElasticity;
AnimBool showInertiaRelatedValues;
void OnEnable()
{
viewport = serializedObject.FindProperty("viewport");
2019-11-16 22:49:21 +08:00
scrollDirection = serializedObject.FindProperty("scrollDirection");
movementType = serializedObject.FindProperty("movementType");
elasticity = serializedObject.FindProperty("elasticity");
scrollSensitivity = serializedObject.FindProperty("scrollSensitivity");
inertia = serializedObject.FindProperty("inertia");
decelerationRate = serializedObject.FindProperty("decelerationRate");
snap = serializedObject.FindProperty("snap");
2019-11-16 22:49:21 +08:00
draggable = serializedObject.FindProperty("draggable");
scrollbar = serializedObject.FindProperty("scrollbar");
showElasticity = new AnimBool(Repaint);
showInertiaRelatedValues = new AnimBool(Repaint);
SetAnimBools(true);
}
void OnDisable()
{
showElasticity.valueChanged.RemoveListener(Repaint);
showInertiaRelatedValues.valueChanged.RemoveListener(Repaint);
}
void SetAnimBools(bool instant)
{
2019-11-16 22:49:21 +08:00
SetAnimBool(showElasticity, !movementType.hasMultipleDifferentValues && movementType.enumValueIndex == (int)MovementType.Elastic, instant);
SetAnimBool(showInertiaRelatedValues, !inertia.hasMultipleDifferentValues && inertia.boolValue, instant);
}
void SetAnimBool(AnimBool a, bool value, bool instant)
{
if (instant)
{
a.value = value;
}
else
{
a.target = value;
}
}
public override void OnInspectorGUI()
{
SetAnimBools(false);
serializedObject.Update();
EditorGUILayout.PropertyField(viewport);
2019-11-16 22:49:21 +08:00
EditorGUILayout.PropertyField(scrollDirection);
EditorGUILayout.PropertyField(movementType);
DrawMovementTypeRelatedValue();
EditorGUILayout.PropertyField(scrollSensitivity);
EditorGUILayout.PropertyField(inertia);
DrawInertiaRelatedValues();
2019-11-16 22:49:21 +08:00
EditorGUILayout.PropertyField(draggable);
EditorGUILayout.PropertyField(scrollbar);
serializedObject.ApplyModifiedProperties();
}
void DrawMovementTypeRelatedValue()
{
using (var group = new EditorGUILayout.FadeGroupScope(showElasticity.faded))
{
if (!group.visible)
{
return;
}
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(elasticity);
}
}
}
void DrawInertiaRelatedValues()
{
using (var group = new EditorGUILayout.FadeGroupScope(showInertiaRelatedValues.faded))
{
2019-11-16 22:49:21 +08:00
if (!group.visible)
{
return;
}
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(decelerationRate);
EditorGUILayout.PropertyField(snap);
}
}
}
}
2019-11-16 22:49:21 +08:00
}