No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SliderEditor.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace UnityEditor.UI
  5. {
  6. [CustomEditor(typeof(Slider), true)]
  7. [CanEditMultipleObjects]
  8. /// <summary>
  9. /// Custom Editor for the Slider Component.
  10. /// Extend this class to write a custom editor for a component derived from Slider.
  11. /// </summary>
  12. public class SliderEditor : SelectableEditor
  13. {
  14. SerializedProperty m_Direction;
  15. SerializedProperty m_FillRect;
  16. SerializedProperty m_HandleRect;
  17. SerializedProperty m_MinValue;
  18. SerializedProperty m_MaxValue;
  19. SerializedProperty m_WholeNumbers;
  20. SerializedProperty m_Value;
  21. SerializedProperty m_OnValueChanged;
  22. protected override void OnEnable()
  23. {
  24. base.OnEnable();
  25. m_FillRect = serializedObject.FindProperty("m_FillRect");
  26. m_HandleRect = serializedObject.FindProperty("m_HandleRect");
  27. m_Direction = serializedObject.FindProperty("m_Direction");
  28. m_MinValue = serializedObject.FindProperty("m_MinValue");
  29. m_MaxValue = serializedObject.FindProperty("m_MaxValue");
  30. m_WholeNumbers = serializedObject.FindProperty("m_WholeNumbers");
  31. m_Value = serializedObject.FindProperty("m_Value");
  32. m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
  33. }
  34. public override void OnInspectorGUI()
  35. {
  36. base.OnInspectorGUI();
  37. EditorGUILayout.Space();
  38. serializedObject.Update();
  39. EditorGUILayout.PropertyField(m_FillRect);
  40. EditorGUILayout.PropertyField(m_HandleRect);
  41. if (m_FillRect.objectReferenceValue != null || m_HandleRect.objectReferenceValue != null)
  42. {
  43. EditorGUI.BeginChangeCheck();
  44. EditorGUILayout.PropertyField(m_Direction);
  45. if (EditorGUI.EndChangeCheck())
  46. {
  47. Undo.RecordObjects(serializedObject.targetObjects, "Change Slider Direction");
  48. Slider.Direction direction = (Slider.Direction)m_Direction.enumValueIndex;
  49. foreach (var obj in serializedObject.targetObjects)
  50. {
  51. Slider slider = obj as Slider;
  52. slider.SetDirection(direction, true);
  53. }
  54. }
  55. EditorGUI.BeginChangeCheck();
  56. float newMin = EditorGUILayout.FloatField("Min Value", m_MinValue.floatValue);
  57. if (EditorGUI.EndChangeCheck())
  58. {
  59. if (m_WholeNumbers.boolValue ? Mathf.Round(newMin) < m_MaxValue.floatValue : newMin < m_MaxValue.floatValue)
  60. {
  61. m_MinValue.floatValue = newMin;
  62. if (m_Value.floatValue < newMin)
  63. m_Value.floatValue = newMin;
  64. }
  65. }
  66. EditorGUI.BeginChangeCheck();
  67. float newMax = EditorGUILayout.FloatField("Max Value", m_MaxValue.floatValue);
  68. if (EditorGUI.EndChangeCheck())
  69. {
  70. if (m_WholeNumbers.boolValue ? Mathf.Round(newMax) > m_MinValue.floatValue : newMax > m_MinValue.floatValue)
  71. {
  72. m_MaxValue.floatValue = newMax;
  73. if (m_Value.floatValue > newMax)
  74. m_Value.floatValue = newMax;
  75. }
  76. }
  77. EditorGUILayout.PropertyField(m_WholeNumbers);
  78. bool areMinMaxEqual = (m_MinValue.floatValue == m_MaxValue.floatValue);
  79. if (areMinMaxEqual)
  80. EditorGUILayout.HelpBox("Min Value and Max Value cannot be equal.", MessageType.Warning);
  81. if (m_WholeNumbers.boolValue)
  82. m_Value.floatValue = Mathf.Round(m_Value.floatValue);
  83. EditorGUI.BeginDisabledGroup(areMinMaxEqual);
  84. EditorGUI.BeginChangeCheck();
  85. EditorGUILayout.Slider(m_Value, m_MinValue.floatValue, m_MaxValue.floatValue);
  86. if (EditorGUI.EndChangeCheck())
  87. {
  88. // Apply the change before sending the event
  89. serializedObject.ApplyModifiedProperties();
  90. foreach (var t in targets)
  91. {
  92. if (t is Slider slider)
  93. {
  94. slider.onValueChanged?.Invoke(slider.value);
  95. }
  96. }
  97. }
  98. EditorGUI.EndDisabledGroup();
  99. bool warning = false;
  100. foreach (var obj in serializedObject.targetObjects)
  101. {
  102. Slider slider = obj as Slider;
  103. Slider.Direction dir = slider.direction;
  104. if (dir == Slider.Direction.LeftToRight || dir == Slider.Direction.RightToLeft)
  105. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnLeft() != null || slider.FindSelectableOnRight() != null));
  106. else
  107. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnDown() != null || slider.FindSelectableOnUp() != null));
  108. }
  109. if (warning)
  110. EditorGUILayout.HelpBox("The selected slider direction conflicts with navigation. Not all navigation options may work.", MessageType.Warning);
  111. // Draw the event notification options
  112. EditorGUILayout.Space();
  113. EditorGUILayout.PropertyField(m_OnValueChanged);
  114. }
  115. else
  116. {
  117. EditorGUILayout.HelpBox("Specify a RectTransform for the slider fill or the slider handle or both. Each must have a parent RectTransform that it can slide within.", MessageType.Info);
  118. }
  119. serializedObject.ApplyModifiedProperties();
  120. }
  121. }
  122. }