Hey guys,
I’m still messing around with custom PropertyDrawers and Inspectors in order to make it a bit more comfortable for me to set stuff in the editor for my game. With your help I was already able to make some progress but I’m stuck on a small bit. I mananged to get my nested Reorderable List working but I cannot get the expand/collapse behavior right. Expanded, everything looks quite nice:
But when I collapse things, the elements of the lists are still kind of displayed and overlapping, or at least the list background is still being displayed. Also when I expand again after this somewhat screwed up state, the lists are not properly displayed in expanded mode and I have to for instance drag them around a bit to force them to look right again. So, possibly I need to do something to force a redraw or something like that, not sure. Here’s what it looks like after collapsing/expanding a bit:
The stuff handling property height and list height seems logical to me as I have implemented it but apparently I’m missing something. Here’s my code for the CustomPropertyDrawer:
[CustomPropertyDrawer(typeof(DestructionPhaseDefinition))]
public class DestructionPhaseDefinitionDrawer : PropertyDrawer
{
private Dictionary<string, ReorderableList> lists = new Dictionary<string, ReorderableList>();
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty prop = property.FindPropertyRelative("destructionLoot");
if (prop != null && prop.isExpanded)
{
if (prop.isExpanded)
{
return GetList(property, prop).GetHeight();
} else
{
return EditorGUIUtility.singleLineHeight;
}
}
return base.GetPropertyHeight(property, label);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
ReorderableList rList = GetList(property, property.FindPropertyRelative("destructionLoot"));
rList.DoList(position);
EditorGUI.indentLevel = indent;
property.serializedObject.ApplyModifiedProperties();
EditorGUI.EndProperty();
}
private ReorderableList GetList(SerializedProperty property, SerializedProperty listProperty)
{
if(lists.ContainsKey(property.propertyPath))
{
return lists[property.propertyPath];
} else
{
ReorderableList list = new ReorderableList(property.serializedObject, listProperty, true, true, true, true);
list.elementHeight = EditorGUIUtility.singleLineHeight;
list.drawHeaderCallback = rect =>
{
var newRect = new Rect(rect.x + 10, rect.y, rect.width - 10, rect.height);
listProperty.isExpanded = EditorGUI.Foldout(newRect, listProperty.isExpanded, listProperty.displayName, true);
};
list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
if(listProperty.isExpanded)
{
EditorGUI.indentLevel = 1;
EditorGUI.PropertyField(rect, listProperty.GetArrayElementAtIndex(index), GUIContent.none);
}
};
list.elementHeightCallback = (int indexer) =>
{
if (!listProperty.isExpanded)
{
return 0;
}
else
{
return list.elementHeight;
}
};
lists.Add(property.propertyPath, list);
return lists[property.propertyPath];
}
}
}
I hope you guys can spot the mistake and give me some pointers, I’d greatly appreciate the help…can’t say I’m enjoying this part of Unity much. Thanks!
PS: I’m using Unity 2020.3.18f1