SliderEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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() && newMin <= m_MaxValue.floatValue)
  57. {
  58. m_MinValue.floatValue = newMin;
  59. }
  60. EditorGUI.BeginChangeCheck();
  61. float newMax = EditorGUILayout.FloatField("Max Value", m_MaxValue.floatValue);
  62. if (EditorGUI.EndChangeCheck() && newMax >= m_MinValue.floatValue)
  63. {
  64. m_MaxValue.floatValue = newMax;
  65. }
  66. EditorGUILayout.PropertyField(m_WholeNumbers);
  67. EditorGUILayout.Slider(m_Value, m_MinValue.floatValue, m_MaxValue.floatValue);
  68. bool warning = false;
  69. foreach (var obj in serializedObject.targetObjects)
  70. {
  71. Slider slider = obj as Slider;
  72. Slider.Direction dir = slider.direction;
  73. if (dir == Slider.Direction.LeftToRight || dir == Slider.Direction.RightToLeft)
  74. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnLeft() != null || slider.FindSelectableOnRight() != null));
  75. else
  76. warning = (slider.navigation.mode != Navigation.Mode.Automatic && (slider.FindSelectableOnDown() != null || slider.FindSelectableOnUp() != null));
  77. }
  78. if (warning)
  79. EditorGUILayout.HelpBox("The selected slider direction conflicts with navigation. Not all navigation options may work.", MessageType.Warning);
  80. // Draw the event notification options
  81. EditorGUILayout.Space();
  82. EditorGUILayout.PropertyField(m_OnValueChanged);
  83. }
  84. else
  85. {
  86. 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);
  87. }
  88. serializedObject.ApplyModifiedProperties();
  89. }
  90. }
  91. }