Skip to content

Commit

Permalink
Usability improvements to inline ScriptableObject editing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChemiKhazi committed Sep 29, 2017
1 parent 409544e commit 012bde4
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions Editor/ReorderableArrayInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public void SetDropHandler(SerializedProperty property, Action<SerializedPropert

private readonly GUILayoutOption uiExpandWidth = GUILayout.ExpandWidth(true);
private readonly GUILayoutOption uiWidth50 = GUILayout.Width(50);
private readonly GUILayoutOption uiHeight2 = GUILayout.Height(2);
private readonly GUIContent labelBtnCreate = new GUIContent("Create");
private GUIStyle styleEditBox;

private readonly List<SortableListData> listIndex = new List<SortableListData>();
private readonly Dictionary<string, Editor> editableIndex = new Dictionary<string, Editor>();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -683,36 +685,59 @@ protected void DrawPropertySortableArray(SerializedProperty property)
}
}
}
// Else try to draw ScriptableObject editor
else if (isScriptableEditor)
{
bool hasSpace = property.HasAttribute<SpaceAttribute>();

// 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--;
}
}
Expand Down

0 comments on commit 012bde4

Please sign in to comment.