From 012bde469b65c96eab6f12a3f8887723bfe20622 Mon Sep 17 00:00:00 2001 From: Jeiel Aranal Date: Fri, 29 Sep 2017 11:03:54 +0800 Subject: [PATCH] Usability improvements to inline ScriptableObject editing --- Editor/ReorderableArrayInspector.cs | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/Editor/ReorderableArrayInspector.cs b/Editor/ReorderableArrayInspector.cs index 136137a..9f9f408 100644 --- a/Editor/ReorderableArrayInspector.cs +++ b/Editor/ReorderableArrayInspector.cs @@ -265,8 +265,8 @@ public void SetDropHandler(SerializedProperty property, Action listIndex = new List(); private readonly Dictionary editableIndex = new Dictionary(); @@ -319,6 +319,7 @@ protected virtual void InitInspector() if (isInitialized && FORCE_INIT == false) return; + styleEditBox = new GUIStyle(EditorStyles.helpBox) { padding = new RectOffset(10, 5, 5, 5) }; FindTargetProperties(); FindContextMenu(); } @@ -668,6 +669,7 @@ protected void DrawPropertySortableArray(SerializedProperty property) Editor scriptableEditor; bool isScriptableEditor = editableIndex.TryGetValue(property.propertyPath, out scriptableEditor); + // Has ReorderableList if (listData != null) { // Try to show the list @@ -683,36 +685,59 @@ protected void DrawPropertySortableArray(SerializedProperty property) } } } + // Else try to draw ScriptableObject editor else if (isScriptableEditor) { + bool hasSpace = property.HasAttribute(); + + // No data in property, draw property field with create button if (scriptableEditor == null) { + bool doCreate; using (new EditorGUILayout.HorizontalScope()) { EditorGUILayout.PropertyField(property, uiExpandWidth); - if (GUILayout.Button(labelBtnCreate, EditorStyles.miniButton, uiWidth50)) + using (new EditorGUILayout.VerticalScope(uiWidth50)) + { + if (hasSpace) GUILayout.Space(10); + doCreate = GUILayout.Button(labelBtnCreate, EditorStyles.miniButton); + } + } + + if (doCreate) + { + Type propType = property.GetTypeReflection(); + var createdAsset = CreateAssetWithSavePrompt(propType, "Assets"); + if (createdAsset != null) { - Type propType = property.GetTypeReflection(); - var createdAsset = CreateAssetWithSavePrompt(propType, "Assets"); - if (createdAsset != null) - property.objectReferenceValue = createdAsset; + property.objectReferenceValue = createdAsset; + property.isExpanded = true; } } } + // Has data in property, draw foldout and editor else { EditorGUILayout.PropertyField(property); + Rect rectFoldout = GUILayoutUtility.GetLastRect(); rectFoldout.width = 20; + if (hasSpace) rectFoldout.yMin += 7; + property.isExpanded = EditorGUI.Foldout(rectFoldout, property.isExpanded, GUIContent.none); if (property.isExpanded) { EditorGUI.indentLevel++; - scriptableEditor.serializedObject.Update(); - scriptableEditor.OnInspectorGUI(); - scriptableEditor.serializedObject.ApplyModifiedProperties(); - GUILayout.Box(GUIContent.none, uiHeight2, uiExpandWidth); + using (new EditorGUILayout.VerticalScope(styleEditBox)) + { + var restoreIndent = EditorGUI.indentLevel; + EditorGUI.indentLevel = 0; + scriptableEditor.serializedObject.Update(); + scriptableEditor.OnInspectorGUI(); + scriptableEditor.serializedObject.ApplyModifiedProperties(); + EditorGUI.indentLevel = restoreIndent; + } EditorGUI.indentLevel--; } }