Skip to content

Commit

Permalink
Improved single line reorderable list drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChemiKhazi committed Jul 28, 2017
1 parent 45c6694 commit d5c9feb
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions Editor/ReorderableArrayInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ protected class SortableListData

private readonly Dictionary<string, ReorderableList> propIndex = new Dictionary<string, ReorderableList>();
private readonly Dictionary<string, Action<SerializedProperty, Object[]>> propDropHandlers = new Dictionary<string, Action<SerializedProperty, Object[]>>();

private readonly Dictionary<string, int> countIndex = new Dictionary<string, int>();

public SortableListData(string parent)
{
Parent = parent;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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))
{
Expand All @@ -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
Expand Down

0 comments on commit d5c9feb

Please sign in to comment.