diff --git a/Editor/ReorderableArrayInspector.cs b/Editor/ReorderableArrayInspector.cs index 1ff4dbc..496a80d 100644 --- a/Editor/ReorderableArrayInspector.cs +++ b/Editor/ReorderableArrayInspector.cs @@ -78,7 +78,8 @@ protected class SortableListData private readonly Dictionary propIndex = new Dictionary(); private readonly Dictionary> propDropHandlers = new Dictionary>(); - + private readonly Dictionary countIndex = new Dictionary(); + public SortableListData(string parent) { Parent = parent; @@ -216,6 +217,33 @@ public bool DoLayoutProperty(SerializedProperty property) return true; } + public int GetElementCount(SerializedProperty property) + { + if (property.arraySize <= 0) + return 0; + + int count; + if (countIndex.TryGetValue(property.propertyPath, out count)) + return count; + + var element = property.GetArrayElementAtIndex(0); + var countElement = element.Copy(); + int childCount = 0; + if (countElement.NextVisible(true)) + { + int depth = countElement.Copy().depth; + do + { + if (countElement.depth != depth) + break; + childCount++; + } while (countElement.NextVisible(false)); + } + + countIndex.Add(property.propertyPath, childCount); + return childCount; + } + public ReorderableList GetPropertyList(SerializedProperty property) { if (propIndex.ContainsKey(property.propertyPath)) @@ -444,7 +472,7 @@ private void HandleReorderableOptions(ReorderableAttribute arrayAttr, Serialized { var list = data.GetPropertyList(property); #if UNITY_5_3_OR_NEWER - list.elementHeightCallback = index => EditorGUIUtility.singleLineHeight; + list.elementHeightCallback = index => EditorGUIUtility.singleLineHeight + 6; list.drawElementBackgroundCallback = (rect, index, active, focused) => { if (focused == false) @@ -454,12 +482,18 @@ private void HandleReorderableOptions(ReorderableAttribute arrayAttr, Serialized GUI.Box(rect, GUIContent.none, styleHighlight); }; #endif + list.drawElementCallback = (rect, index, active, focused) => { var element = property.GetArrayElementAtIndex(index); element.isExpanded = false; - int childCount = element.Copy().CountRemaining(); - childCount -= (property.arraySize - 1) - index; + + int childCount = data.GetElementCount(property); + if (childCount < 1) + return; + + rect.y += 3; + rect.height -= 6; if (element.NextVisible(true)) { @@ -476,7 +510,7 @@ private void HandleReorderableOptions(ReorderableAttribute arrayAttr, Serialized { if (element.depth != depth) break; - //EditorGUI.PropertyField(childRect, element, false); + if (childCount <= 2) EditorGUI.PropertyField(childRect, element, false); else