ScrollerEditor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /// Credit setchi (https://github.com/setchi)
  2. /// Sourced from - https://github.com/setchi/FancyScrollView
  3. using UnityEditor;
  4. using UnityEditor.AnimatedValues;
  5. // For maintenance, every new [SerializeField] variable in Scroller must be declared here
  6. namespace UnityEngine.UI.Extensions
  7. {
  8. [CustomEditor(typeof(Scroller))]
  9. [CanEditMultipleObjects]
  10. public class ScrollerEditor : Editor
  11. {
  12. SerializedProperty viewport;
  13. SerializedProperty scrollDirection;
  14. SerializedProperty movementType;
  15. SerializedProperty elasticity;
  16. SerializedProperty scrollSensitivity;
  17. SerializedProperty inertia;
  18. SerializedProperty decelerationRate;
  19. SerializedProperty snap;
  20. SerializedProperty snapEnable;
  21. SerializedProperty snapVelocityThreshold;
  22. SerializedProperty snapDuration;
  23. SerializedProperty snapEasing;
  24. SerializedProperty draggable;
  25. SerializedProperty scrollbar;
  26. AnimBool showElasticity;
  27. AnimBool showInertiaRelatedValues;
  28. AnimBool showSnapEnableRelatedValues;
  29. void OnEnable()
  30. {
  31. viewport = serializedObject.FindProperty("viewport");
  32. scrollDirection = serializedObject.FindProperty("scrollDirection");
  33. movementType = serializedObject.FindProperty("movementType");
  34. elasticity = serializedObject.FindProperty("elasticity");
  35. scrollSensitivity = serializedObject.FindProperty("scrollSensitivity");
  36. inertia = serializedObject.FindProperty("inertia");
  37. decelerationRate = serializedObject.FindProperty("decelerationRate");
  38. snap = serializedObject.FindProperty("snap");
  39. snapEnable = serializedObject.FindProperty("snap.Enable");
  40. snapVelocityThreshold = serializedObject.FindProperty("snap.VelocityThreshold");
  41. snapDuration = serializedObject.FindProperty("snap.Duration");
  42. snapEasing = serializedObject.FindProperty("snap.Easing");
  43. draggable = serializedObject.FindProperty("draggable");
  44. scrollbar = serializedObject.FindProperty("scrollbar");
  45. showElasticity = new AnimBool(Repaint);
  46. showInertiaRelatedValues = new AnimBool(Repaint);
  47. showSnapEnableRelatedValues = new AnimBool(Repaint);
  48. SetAnimBools(true);
  49. }
  50. void OnDisable()
  51. {
  52. showElasticity.valueChanged.RemoveListener(Repaint);
  53. showInertiaRelatedValues.valueChanged.RemoveListener(Repaint);
  54. showSnapEnableRelatedValues.valueChanged.RemoveListener(Repaint);
  55. }
  56. void SetAnimBools(bool instant)
  57. {
  58. SetAnimBool(showElasticity, !movementType.hasMultipleDifferentValues && movementType.enumValueIndex == (int)MovementType.Elastic, instant);
  59. SetAnimBool(showInertiaRelatedValues, !inertia.hasMultipleDifferentValues && inertia.boolValue, instant);
  60. SetAnimBool(showSnapEnableRelatedValues, !snapEnable.hasMultipleDifferentValues && snapEnable.boolValue, instant);
  61. }
  62. void SetAnimBool(AnimBool a, bool value, bool instant)
  63. {
  64. if (instant)
  65. {
  66. a.value = value;
  67. }
  68. else
  69. {
  70. a.target = value;
  71. }
  72. }
  73. public override void OnInspectorGUI()
  74. {
  75. SetAnimBools(false);
  76. serializedObject.Update();
  77. EditorGUILayout.PropertyField(viewport);
  78. EditorGUILayout.PropertyField(scrollDirection);
  79. EditorGUILayout.PropertyField(movementType);
  80. DrawMovementTypeRelatedValue();
  81. EditorGUILayout.PropertyField(scrollSensitivity);
  82. EditorGUILayout.PropertyField(inertia);
  83. DrawInertiaRelatedValues();
  84. EditorGUILayout.PropertyField(draggable);
  85. EditorGUILayout.PropertyField(scrollbar);
  86. serializedObject.ApplyModifiedProperties();
  87. }
  88. void DrawMovementTypeRelatedValue()
  89. {
  90. using (var group = new EditorGUILayout.FadeGroupScope(showElasticity.faded))
  91. {
  92. if (!group.visible)
  93. {
  94. return;
  95. }
  96. using (new EditorGUI.IndentLevelScope())
  97. {
  98. EditorGUILayout.PropertyField(elasticity);
  99. }
  100. }
  101. }
  102. void DrawInertiaRelatedValues()
  103. {
  104. using (var group = new EditorGUILayout.FadeGroupScope(showInertiaRelatedValues.faded))
  105. {
  106. if (!group.visible)
  107. {
  108. return;
  109. }
  110. using (new EditorGUI.IndentLevelScope())
  111. {
  112. EditorGUILayout.PropertyField(decelerationRate);
  113. EditorGUILayout.PropertyField(snap);
  114. using (new EditorGUI.IndentLevelScope())
  115. {
  116. DrawSnapRelatedValues();
  117. }
  118. }
  119. }
  120. }
  121. void DrawSnapRelatedValues()
  122. {
  123. if (snap.isExpanded)
  124. {
  125. EditorGUILayout.PropertyField(snapEnable);
  126. using (var group = new EditorGUILayout.FadeGroupScope(showSnapEnableRelatedValues.faded))
  127. {
  128. if (!group.visible)
  129. {
  130. return;
  131. }
  132. EditorGUILayout.PropertyField(snapVelocityThreshold);
  133. EditorGUILayout.PropertyField(snapDuration);
  134. EditorGUILayout.PropertyField(snapEasing);
  135. }
  136. }
  137. }
  138. }
  139. }