暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ScrollRectEditor.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor.AnimatedValues;
  4. namespace UnityEditor.UI
  5. {
  6. [CustomEditor(typeof(ScrollRect), true)]
  7. [CanEditMultipleObjects]
  8. /// <summary>
  9. /// Custom Editor for the ScrollRect Component.
  10. /// Extend this class to write a custom editor for a component derived from ScrollRect.
  11. /// </summary>
  12. public class ScrollRectEditor : Editor
  13. {
  14. SerializedProperty m_Content;
  15. SerializedProperty m_Horizontal;
  16. SerializedProperty m_Vertical;
  17. SerializedProperty m_MovementType;
  18. SerializedProperty m_Elasticity;
  19. SerializedProperty m_Inertia;
  20. SerializedProperty m_DecelerationRate;
  21. SerializedProperty m_ScrollSensitivity;
  22. SerializedProperty m_Viewport;
  23. SerializedProperty m_HorizontalScrollbar;
  24. SerializedProperty m_VerticalScrollbar;
  25. SerializedProperty m_HorizontalScrollbarVisibility;
  26. SerializedProperty m_VerticalScrollbarVisibility;
  27. SerializedProperty m_HorizontalScrollbarSpacing;
  28. SerializedProperty m_VerticalScrollbarSpacing;
  29. SerializedProperty m_OnValueChanged;
  30. AnimBool m_ShowElasticity;
  31. AnimBool m_ShowDecelerationRate;
  32. bool m_ViewportIsNotChild, m_HScrollbarIsNotChild, m_VScrollbarIsNotChild;
  33. static string s_HError = "For this visibility mode, the Viewport property and the Horizontal Scrollbar property both needs to be set to a Rect Transform that is a child to the Scroll Rect.";
  34. static string s_VError = "For this visibility mode, the Viewport property and the Vertical Scrollbar property both needs to be set to a Rect Transform that is a child to the Scroll Rect.";
  35. protected virtual void OnEnable()
  36. {
  37. m_Content = serializedObject.FindProperty("m_Content");
  38. m_Horizontal = serializedObject.FindProperty("m_Horizontal");
  39. m_Vertical = serializedObject.FindProperty("m_Vertical");
  40. m_MovementType = serializedObject.FindProperty("m_MovementType");
  41. m_Elasticity = serializedObject.FindProperty("m_Elasticity");
  42. m_Inertia = serializedObject.FindProperty("m_Inertia");
  43. m_DecelerationRate = serializedObject.FindProperty("m_DecelerationRate");
  44. m_ScrollSensitivity = serializedObject.FindProperty("m_ScrollSensitivity");
  45. m_Viewport = serializedObject.FindProperty("m_Viewport");
  46. m_HorizontalScrollbar = serializedObject.FindProperty("m_HorizontalScrollbar");
  47. m_VerticalScrollbar = serializedObject.FindProperty("m_VerticalScrollbar");
  48. m_HorizontalScrollbarVisibility = serializedObject.FindProperty("m_HorizontalScrollbarVisibility");
  49. m_VerticalScrollbarVisibility = serializedObject.FindProperty("m_VerticalScrollbarVisibility");
  50. m_HorizontalScrollbarSpacing = serializedObject.FindProperty("m_HorizontalScrollbarSpacing");
  51. m_VerticalScrollbarSpacing = serializedObject.FindProperty("m_VerticalScrollbarSpacing");
  52. m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
  53. m_ShowElasticity = new AnimBool(Repaint);
  54. m_ShowDecelerationRate = new AnimBool(Repaint);
  55. SetAnimBools(true);
  56. }
  57. protected virtual void OnDisable()
  58. {
  59. m_ShowElasticity.valueChanged.RemoveListener(Repaint);
  60. m_ShowDecelerationRate.valueChanged.RemoveListener(Repaint);
  61. }
  62. void SetAnimBools(bool instant)
  63. {
  64. SetAnimBool(m_ShowElasticity, !m_MovementType.hasMultipleDifferentValues && m_MovementType.enumValueIndex == (int)ScrollRect.MovementType.Elastic, instant);
  65. SetAnimBool(m_ShowDecelerationRate, !m_Inertia.hasMultipleDifferentValues && m_Inertia.boolValue == true, instant);
  66. }
  67. void SetAnimBool(AnimBool a, bool value, bool instant)
  68. {
  69. if (instant)
  70. a.value = value;
  71. else
  72. a.target = value;
  73. }
  74. void CalculateCachedValues()
  75. {
  76. m_ViewportIsNotChild = false;
  77. m_HScrollbarIsNotChild = false;
  78. m_VScrollbarIsNotChild = false;
  79. if (targets.Length == 1)
  80. {
  81. Transform transform = ((ScrollRect)target).transform;
  82. if (m_Viewport.objectReferenceValue == null || ((RectTransform)m_Viewport.objectReferenceValue).transform.parent != transform)
  83. m_ViewportIsNotChild = true;
  84. if (m_HorizontalScrollbar.objectReferenceValue == null || ((Scrollbar)m_HorizontalScrollbar.objectReferenceValue).transform.parent != transform)
  85. m_HScrollbarIsNotChild = true;
  86. if (m_VerticalScrollbar.objectReferenceValue == null || ((Scrollbar)m_VerticalScrollbar.objectReferenceValue).transform.parent != transform)
  87. m_VScrollbarIsNotChild = true;
  88. }
  89. }
  90. public override void OnInspectorGUI()
  91. {
  92. SetAnimBools(false);
  93. serializedObject.Update();
  94. // Once we have a reliable way to know if the object changed, only re-cache in that case.
  95. CalculateCachedValues();
  96. EditorGUILayout.PropertyField(m_Content);
  97. EditorGUILayout.PropertyField(m_Horizontal);
  98. EditorGUILayout.PropertyField(m_Vertical);
  99. EditorGUILayout.PropertyField(m_MovementType);
  100. if (EditorGUILayout.BeginFadeGroup(m_ShowElasticity.faded))
  101. {
  102. EditorGUI.indentLevel++;
  103. EditorGUILayout.PropertyField(m_Elasticity);
  104. EditorGUI.indentLevel--;
  105. }
  106. EditorGUILayout.EndFadeGroup();
  107. EditorGUILayout.PropertyField(m_Inertia);
  108. if (EditorGUILayout.BeginFadeGroup(m_ShowDecelerationRate.faded))
  109. {
  110. EditorGUI.indentLevel++;
  111. EditorGUILayout.PropertyField(m_DecelerationRate);
  112. EditorGUI.indentLevel--;
  113. }
  114. EditorGUILayout.EndFadeGroup();
  115. EditorGUILayout.PropertyField(m_ScrollSensitivity);
  116. EditorGUILayout.Space();
  117. EditorGUILayout.PropertyField(m_Viewport);
  118. EditorGUILayout.PropertyField(m_HorizontalScrollbar);
  119. if (m_HorizontalScrollbar.objectReferenceValue && !m_HorizontalScrollbar.hasMultipleDifferentValues)
  120. {
  121. EditorGUI.indentLevel++;
  122. EditorGUILayout.PropertyField(m_HorizontalScrollbarVisibility, EditorGUIUtility.TrTextContent("Visibility"));
  123. if ((ScrollRect.ScrollbarVisibility)m_HorizontalScrollbarVisibility.enumValueIndex == ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport
  124. && !m_HorizontalScrollbarVisibility.hasMultipleDifferentValues)
  125. {
  126. if (m_ViewportIsNotChild || m_HScrollbarIsNotChild)
  127. EditorGUILayout.HelpBox(s_HError, MessageType.Error);
  128. EditorGUILayout.PropertyField(m_HorizontalScrollbarSpacing, EditorGUIUtility.TrTextContent("Spacing"));
  129. }
  130. EditorGUI.indentLevel--;
  131. }
  132. EditorGUILayout.PropertyField(m_VerticalScrollbar);
  133. if (m_VerticalScrollbar.objectReferenceValue && !m_VerticalScrollbar.hasMultipleDifferentValues)
  134. {
  135. EditorGUI.indentLevel++;
  136. EditorGUILayout.PropertyField(m_VerticalScrollbarVisibility, EditorGUIUtility.TrTextContent("Visibility"));
  137. if ((ScrollRect.ScrollbarVisibility)m_VerticalScrollbarVisibility.enumValueIndex == ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport
  138. && !m_VerticalScrollbarVisibility.hasMultipleDifferentValues)
  139. {
  140. if (m_ViewportIsNotChild || m_VScrollbarIsNotChild)
  141. EditorGUILayout.HelpBox(s_VError, MessageType.Error);
  142. EditorGUILayout.PropertyField(m_VerticalScrollbarSpacing, EditorGUIUtility.TrTextContent("Spacing"));
  143. }
  144. EditorGUI.indentLevel--;
  145. }
  146. EditorGUILayout.Space();
  147. EditorGUILayout.PropertyField(m_OnValueChanged);
  148. serializedObject.ApplyModifiedProperties();
  149. }
  150. }
  151. }