| | 1 | | using UnityEngine; |
| | 2 | | #if UNITY_EDITOR |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEditor; |
| | 6 | |
|
| | 7 | | #endif |
| | 8 | |
|
| | 9 | | #if UNITY_EDITOR |
| | 10 | | /// <summary> |
| | 11 | | /// Draws the property field for any field marked with ExpandableAttribute. |
| | 12 | | /// </summary> |
| | 13 | | [CustomPropertyDrawer(typeof(ExpandableAttribute), true)] |
| | 14 | | public class ExpandableAttributeDrawer : PropertyDrawer |
| | 15 | | { |
| | 16 | | // Use the following area to change the style of the expandable ScriptableObject drawers; |
| | 17 | |
|
| | 18 | | #region Style Setup |
| | 19 | |
|
| | 20 | | private enum BackgroundStyles |
| | 21 | | { |
| | 22 | | None, |
| | 23 | | HelpBox, |
| | 24 | | Darken, |
| | 25 | | Lighten |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Whether the default editor Script field should be shown. |
| | 30 | | /// </summary> |
| 0 | 31 | | private static bool SHOW_SCRIPT_FIELD = false; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The spacing on the inside of the background rect. |
| | 35 | | /// </summary> |
| 0 | 36 | | private static float INNER_SPACING = 6.0f; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The spacing on the outside of the background rect. |
| | 40 | | /// </summary> |
| 0 | 41 | | private static float OUTER_SPACING = 4.0f; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The style the background uses. |
| | 45 | | /// </summary> |
| 0 | 46 | | private static BackgroundStyles BACKGROUND_STYLE = BackgroundStyles.HelpBox; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// The colour that is used to darken the background. |
| | 50 | | /// </summary> |
| 0 | 51 | | private static Color DARKEN_COLOUR = new Color(0.0f, 0.0f, 0.0f, 0.2f); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The colour that is used to lighten the background. |
| | 55 | | /// </summary> |
| 0 | 56 | | private static Color LIGHTEN_COLOUR = new Color(1.0f, 1.0f, 1.0f, 0.2f); |
| | 57 | |
|
| | 58 | | #endregion |
| | 59 | |
|
| | 60 | | public override float GetPropertyHeight(SerializedProperty property, GUIContent label) |
| | 61 | | { |
| 0 | 62 | | float totalHeight = 0.0f; |
| | 63 | |
|
| 0 | 64 | | totalHeight += EditorGUIUtility.singleLineHeight; |
| | 65 | |
|
| 0 | 66 | | if (property.objectReferenceValue == null) |
| 0 | 67 | | return totalHeight; |
| | 68 | |
|
| 0 | 69 | | if (!property.isExpanded) |
| 0 | 70 | | return totalHeight; |
| | 71 | |
|
| 0 | 72 | | SerializedObject targetObject = new SerializedObject(property.objectReferenceValue); |
| | 73 | |
|
| 0 | 74 | | if (targetObject == null) |
| 0 | 75 | | return totalHeight; |
| | 76 | |
|
| 0 | 77 | | SerializedProperty field = targetObject.GetIterator(); |
| | 78 | |
|
| 0 | 79 | | field.NextVisible(true); |
| | 80 | |
|
| 0 | 81 | | if (SHOW_SCRIPT_FIELD) |
| | 82 | | { |
| 0 | 83 | | totalHeight += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | while (field.NextVisible(false)) |
| | 87 | | { |
| 0 | 88 | | totalHeight += EditorGUI.GetPropertyHeight(field, true) + EditorGUIUtility.standardVerticalSpacing; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | totalHeight += INNER_SPACING * 2; |
| 0 | 92 | | totalHeight += OUTER_SPACING * 2; |
| | 93 | |
|
| 0 | 94 | | return totalHeight; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
| | 98 | | { |
| 0 | 99 | | Rect fieldRect = new Rect(position); |
| 0 | 100 | | fieldRect.height = EditorGUIUtility.singleLineHeight; |
| | 101 | |
|
| 0 | 102 | | EditorGUI.PropertyField(fieldRect, property, label, true); |
| | 103 | |
|
| 0 | 104 | | if (property.objectReferenceValue == null) |
| 0 | 105 | | return; |
| | 106 | |
|
| 0 | 107 | | property.isExpanded = EditorGUI.Foldout(fieldRect, property.isExpanded, GUIContent.none, true); |
| | 108 | |
|
| 0 | 109 | | if (!property.isExpanded) |
| 0 | 110 | | return; |
| | 111 | |
|
| 0 | 112 | | SerializedObject targetObject = new SerializedObject(property.objectReferenceValue); |
| | 113 | |
|
| 0 | 114 | | if (targetObject == null) |
| 0 | 115 | | return; |
| | 116 | |
|
| | 117 | |
|
| | 118 | | #region Format Field Rects |
| | 119 | |
|
| 0 | 120 | | List<Rect> propertyRects = new List<Rect>(); |
| 0 | 121 | | Rect marchingRect = new Rect(fieldRect); |
| | 122 | |
|
| 0 | 123 | | Rect bodyRect = new Rect(fieldRect); |
| 0 | 124 | | bodyRect.xMin += EditorGUI.indentLevel * 14; |
| 0 | 125 | | bodyRect.yMin += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing |
| | 126 | | + OUTER_SPACING; |
| | 127 | |
|
| 0 | 128 | | SerializedProperty field = targetObject.GetIterator(); |
| 0 | 129 | | field.NextVisible(true); |
| | 130 | |
|
| 0 | 131 | | marchingRect.y += INNER_SPACING + OUTER_SPACING; |
| | 132 | |
|
| 0 | 133 | | if (SHOW_SCRIPT_FIELD) |
| | 134 | | { |
| 0 | 135 | | propertyRects.Add(marchingRect); |
| 0 | 136 | | marchingRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | while (field.NextVisible(false)) |
| | 140 | | { |
| 0 | 141 | | marchingRect.y += marchingRect.height + EditorGUIUtility.standardVerticalSpacing; |
| 0 | 142 | | marchingRect.height = EditorGUI.GetPropertyHeight(field, true); |
| 0 | 143 | | propertyRects.Add(marchingRect); |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | marchingRect.y += INNER_SPACING; |
| | 147 | |
|
| 0 | 148 | | bodyRect.yMax = marchingRect.yMax; |
| | 149 | |
|
| | 150 | | #endregion |
| | 151 | |
|
| 0 | 152 | | DrawBackground(bodyRect); |
| | 153 | |
|
| | 154 | | #region Draw Fields |
| | 155 | |
|
| 0 | 156 | | EditorGUI.indentLevel++; |
| | 157 | |
|
| 0 | 158 | | int index = 0; |
| 0 | 159 | | field = targetObject.GetIterator(); |
| 0 | 160 | | field.NextVisible(true); |
| | 161 | |
|
| 0 | 162 | | if (SHOW_SCRIPT_FIELD) |
| | 163 | | { |
| | 164 | | //Show the disabled script field |
| 0 | 165 | | EditorGUI.BeginDisabledGroup(true); |
| 0 | 166 | | EditorGUI.PropertyField(propertyRects[index], field, true); |
| 0 | 167 | | EditorGUI.EndDisabledGroup(); |
| 0 | 168 | | index++; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | //Replacement for "editor.OnInspectorGUI ();" so we have more control on how we draw the editor |
| 0 | 172 | | while (field.NextVisible(false)) |
| | 173 | | { |
| | 174 | | try |
| | 175 | | { |
| 0 | 176 | | EditorGUI.PropertyField(propertyRects[index], field, true); |
| 0 | 177 | | } |
| 0 | 178 | | catch (StackOverflowException) |
| | 179 | | { |
| 0 | 180 | | field.objectReferenceValue = null; |
| 0 | 181 | | Debug.LogError("Detected self-nesting cauisng a StackOverflowException, avoid using the same " + |
| | 182 | | "object iside a nested structure."); |
| 0 | 183 | | } |
| | 184 | |
|
| 0 | 185 | | index++; |
| | 186 | | } |
| | 187 | |
|
| 0 | 188 | | targetObject.ApplyModifiedProperties(); |
| | 189 | |
|
| 0 | 190 | | EditorGUI.indentLevel--; |
| | 191 | |
|
| | 192 | | #endregion |
| | 193 | |
|
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// Draws the Background |
| | 198 | | /// </summary> |
| | 199 | | /// <param name="rect">The Rect where the background is drawn.</param> |
| | 200 | | private void DrawBackground(Rect rect) |
| | 201 | | { |
| 0 | 202 | | switch (BACKGROUND_STYLE) |
| | 203 | | { |
| | 204 | |
|
| | 205 | | case BackgroundStyles.HelpBox: |
| 0 | 206 | | EditorGUI.HelpBox(rect, "", MessageType.None); |
| 0 | 207 | | break; |
| | 208 | |
|
| | 209 | | case BackgroundStyles.Darken: |
| 0 | 210 | | EditorGUI.DrawRect(rect, DARKEN_COLOUR); |
| 0 | 211 | | break; |
| | 212 | |
|
| | 213 | | case BackgroundStyles.Lighten: |
| 0 | 214 | | EditorGUI.DrawRect(rect, LIGHTEN_COLOUR); |
| | 215 | | break; |
| | 216 | | } |
| 0 | 217 | | } |
| | 218 | | } |
| | 219 | | #endif |