ListView does not grow to fill available height in custom editor

Why doesn’t the following piece of code work?

using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UIElements;

[CustomEditor(typeof(CardSet), editorForChildClasses: true)]
public class CardSetEditor : Editor
{
    private List<string> cards = new List<string>() { "A", "B", "C" };

    public override VisualElement CreateInspectorGUI()
    {
        var list = new ListView(cards, 30, MakeItem, BindItem);
        list.style.flexGrow = 1f;
        return list;
    }

    private void BindItem(VisualElement label, int index)
    {
        ((Label)label).text = cards[index];
    }

    private VisualElement MakeItem()
    {
        return new Label();
    }
}

Following the documentation, I would expect the ListView to fill the entire space of the custom editor in the inspector window, but for some reason, the parent VisualElements (which I do not have control over), all have zero height, so nothing is visible. Do I have to set a fixed size for my inspector? But if so, why can’t I let Unity calculate the height as it did with IMGUI previously?

This is a known issue with ListViews within the inspector right now. As a workaround, you can set a minimumHeight so it will be displayed at that size for now and will expand in the future when we fix this.

2 Likes

Just some more context:

This will only set the style of your ListView, which inside an Editor element (that has the Component name, “CardSet”, and the UI to remove/reorder the component). The Editor element itself is not being stretched. It’s job is to grow with its content (your custom inspector). But flexGrow=1 reverses the roles - the child now wants to grow with the parent. This is what is currently not easily doable in the Inspector, via Editor.CreateInspectorGUI().

Now, for your particular case, one workaround which might cause other issues but worth a try is to navigate up the UI hierarchy, find the Editor element and change its style to flexGrow=1. You can’t don this directly in CreateInspectorGUI() because you’ve not been added to the tree yet, but you can register for the AttachToPanelEvent on your ListView which will fire as soon as the ListView is added to the tree.

2 Likes