Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to create a List<T> in UIElements?

Discussion in 'UIElements' started by KosaJr, Jul 18, 2019.

  1. KosaJr

    KosaJr

    Joined:
    Jan 3, 2017
    Posts:
    8
    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:
    24
    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:
    8
    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