Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to create a List<T> in UIElements?

Discussion in 'UI Toolkit' started by KosaJr, Jul 18, 2019.

  1. KosaJr

    KosaJr

    Joined:
    Jan 3, 2017
    Posts:
    10
    Is it possible to create a List<T> with UIElements, when the List<T> 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):
    Code (CSharp):
    1. public class MyEditor : EditorWindow
    2. {
    3.       public List<string> someStringList = new List<string>();
    4.  
    5.       // Rest of EditorWindow code...
    6. }
     
  2. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    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>
    .
     
  3. KosaJr

    KosaJr

    Joined:
    Jan 3, 2017
    Posts:
    10
    Yes, I know that :) I mean, how to make this list visible in my editor (not inspector) Window in Unity.
    For example (in uxml file) : <TextField class="tf" name="myname" /> creates a Text field. How to create a list/array in that way?

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

    Attached Files:

    Last edited: Jul 19, 2019
    SBods9A and ChopSushi like this.
  4. unity_0jLKm6lYlxWUnw

    unity_0jLKm6lYlxWUnw

    Joined:
    Dec 14, 2021
    Posts:
    1
    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:

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <engine:UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:engine="UnityEngine.UIElements"
    5.     xmlns:editor="UnityEditor.UIElements"
    6.     xsi:noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd"
    7. >
    8.     <ue:PropertyField name="stringList" label="Unaccepted climates: " binding-path="spriteList"/>
    9.  
    10. </engine:UXML>
    Code (CSharp):
    1. public List<string> spriteList = new List<string>();
    2.  
    3.     public void CreateGUI()
    4.     {
    5.         // Each editor window contains a root VisualElement object
    6.         VisualElement root = rootVisualElement;
    7.  
    8.         // Import UXML
    9.         var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(globalPath+ "SpriteSheetImporter.uxml");
    10.         VisualElement labelFromUXML = visualTree.Instantiate();
    11.         root.Add(labelFromUXML);
    12.        
    13.         rootVisualElement.Q<PropertyField>("stringList").Bind(new SerializedObject(this));
    14.     }
    Am I missing something ?
     
  5. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    735
    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.
     
    Last edited: Dec 14, 2022