SliderEditor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Slider.Direction direction = (Slider.Direction)m_Direction.enumValueIndex;
  48. foreach (var obj in serializedObject.targetObjects)
  49. {
  50. Slider slider = obj as Slider;
  51. slider.SetDirection(direction, true);
  52. }
  53. }
  54. EditorGUI.BeginChangeCheck();
  55. float newMin = EditorGUILayout.FloatField("Min Value", m_MinValue.floatValue);
  56. if(EditorGUI.EndChangeCheck())
  57. {
  58. if (m_WholeNumbers.boolValue ? Mathf.Round(newMin) < m_MaxValue.floatValue : newMin < m_MaxValue.floatValue)
  59. m_MinValue.floatValue = newMin;
  60. }
  61. EditorGUI.BeginChangeCheck();
  62. float newMax = EditorGUILayout.FloatField("Max Value", m_MaxValue.floatValue);
  63. if (EditorGUI.EndChangeCheck())
  64. {
  65. if (m_WholeNumbers.boolValue ? Mathf.Round(newMax) > m_MinValue.floatValue : newMax > m_MinValue.floatValue)
  66. m_MaxValue.floatValue = newMax;
  67. }
  68. EditorGUILayout.PropertyField(m_WholeNumbers);
  69. bool areMinMaxEqual = (m_MinValue.floatValue == m_MaxValue.floatValue);
  70. if (areMinMaxEqual)
  71. EditorGUILayout.HelpBox("Min Value and Max Value cannot be equal.", MessageType.Warning);
  72. EditorGUI.BeginDisabledGroup(areMinMaxEqual);
  73. EditorGUILayout.Slider(m_Value, m_MinValue.floatValue, m_MaxValue.floatValue);
  74. EditorGUI.EndDisabledGroup();
  75. bool warning = false;
  76. foreach (var obj in serializedObject.targetObjects)
  77. {
  78. Slider slider = obj as Slider;
  79. Slider.Direction dir = slider.direction;
  80. if (dir == Slider.Direction.LeftToRight || dir == Slider.Direction.RightToLeft)
  81. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnLeft() != null || slider.FindSelectableOnRight() != null));
  82. else
  83. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnDown() != null || slider.FindSelectableOnUp() != null));
  84. }
  85. if (warning)
  86. EditorGUILayout.HelpBox("The selected slider direction conflicts with navigation. Not all navigation options may work.", MessageType.Warning);
  87. // Draw the event notification options
  88. EditorGUILayout.Space();
  89. EditorGUILayout.PropertyField(m_OnValueChanged);
  90. }
  91. else
  92. {
  93. 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);
  94. }
  95. serializedObject.ApplyModifiedProperties();
  96. }
  97. }
  98. }