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