- Home /
How to prevent overlap when using nested arrays in CustomPropertyDrawers?
Hey guys,
I have been struggling with custom PropertyDrawers and specifically displaying nested arrays properly for a while now and it seems I'm not getting any further on my own. So, I really hope you guys can help me with my problem. Please check the relevant code bits below.
Here is my struct with an array property that should be covered by a custom PropertyDrawer:
[Serializable]
public struct DestructionPhaseDefinition
{
public int[] destructionLoot;
}
Here is a scriptable object holding an array of these structs:
public class HarvestPlantData : ScriptableObject
{
public DestructionPhaseDefinition[] destructionPhases;
}
And this is my PropertyDrawer so far:
[CustomPropertyDrawer(typeof(DestructionPhaseDefinition))]
public class DestructionPhaseDefinitionDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
GUIContent sampleGUIContent = new GUIContent("Loot");
EditorGUI.PropertyField(position, property.FindPropertyRelative("destructionLoot"), sampleGUIContent, true);
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
As you can see, the scriptable object has an array of the struct and within the struct there is another array. While the outer array works fine, I'm getting overlap when expanding/folding the inner array (destructionLoot). Actually there is no reaction at all to expanding/folding these and it looks like this:
What I would expect is for the other elements of the outer array to "move down" in order not to overlap the expanded inner array. I know that this is related to the drawing rect height that these properties are assigned but I just can't figure out how to set this correctly. So, I hope that I'm just missing a few bits and that what I'm trying to do is possible. I have been looking around for examples etc and tried to integrate a couple of solutions but I couldn't get anything to work which might be due to my limited understanding of that whole custom inspector stuff. Btw, I'm using Unity 2020.3.18f1.
Thank you very much in advance!
Answer by Bonfire-Boy · Sep 14, 2021 at 04:20 PM
Your drawer is missing a function telling the editor how much space to leave. You want to be looking at overriding PropertyDrawer.GetPropertyHeight()
This should work as a starting point. Try adding it to DestructionPhaseDefinitionDrawer
...
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var typeProperty = property.FindPropertyRelative("destructionLoot");
if (!typeProperty.isExpanded)
{
return EditorGUIUtility.singleLineHeight; // single line if not expanded
}
else
{
return (2 + typeProperty.arraySize) * ( EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing); // if expanded, one line for the label, one for the size, and one for each element
}
}