How to create a List<T> in UIElements?

Is it possible to create a List with UIElements, when the List is in the same class as MyEditor? I want to create/map/bind it in .uxml file, but dont know how (or is it even possible).

in example (blabla code):

public class MyEditor : EditorWindow
{
      public List<string> someStringList = new List<string>();

      // Rest of EditorWindow code...
}

The ability to declare a generic list in a class is independent from UIElements. Just add using System.Collections.Generic; at the top of your file, or use the full name of System.Collections.Generic.List<T>.

Yes, I know that :slight_smile: I mean, how to make this list visible in my editor (not inspector) Window in Unity.
For example (in uxml file) : creates a Text field. How to create a list/array in that way?

== SOLVED ==
I figured it out! Simply I just need this:
rootVisualElement.Q<PropertyField>("stringList").Bind(new SerializedObject(this)); and properly configured UXML:
<ue:PropertyField name="stringList" label="Unaccepted climates: " binding-path="stringes"/>, where “stringes” is my List variable name located in MyEditor.

4762955--452759--ListView.jpg

3 Likes

Hi. I’m trying to achieve the same thing. Unfortunately I cannot make this work on my project. I get hit with a NullReferenceException even though I’m using the exact same name in both scripts. Here are the full code:

<?xml version="1.0" encoding="utf-8"?>
<engine:UXML
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:engine="UnityEngine.UIElements"
    xmlns:editor="UnityEditor.UIElements"
    xsi:noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd"
>
    <ue:PropertyField name="stringList" label="Unaccepted climates: " binding-path="spriteList"/>

</engine:UXML>
public List<string> spriteList = new List<string>();

    public void CreateGUI()
    {
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;

        // Import UXML
        var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(globalPath+ "SpriteSheetImporter.uxml");
        VisualElement labelFromUXML = visualTree.Instantiate();
        root.Add(labelFromUXML);
       
        rootVisualElement.Q<PropertyField>("stringList").Bind(new SerializedObject(this));
    }

Am I missing something ?

Edit: nevermind my previous message, your Binding code looks correct.

Can you share the exact stacktrace for the null reference?

It looks like you should use <editor:PropertyField> instead of <ue: PropertyField/> in the UXML.