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

Creating a list in a custom editor window

Discussion in 'UI Toolkit' started by ggreer, Jun 8, 2022.

  1. ggreer

    ggreer

    Joined:
    May 22, 2019
    Posts:
    5
    Hi all, working on my first real custom tool and might have been too ambitious. The default import settings in my project for animations is "Loop Time" set to false, but for a specific subset of animations I want to set loop time to true. Going one by one is fine but over time it's annoying, so I figured I'd automate it. I have the code that flips that bool working fine but here's my question:

    Using UI Toolkit, is there a simple way to create a list of objects that works the same way as in a monobehavior? i.e. +/- button or an int for list size that dynamically adds object fields.

    If not, what would be an appropriate setup that would give me that functionality?

    My target is to be able to grab a bunch of animation clips and batch toggle "loop time" across the whole list.
     
  2. ggreer

    ggreer

    Joined:
    May 22, 2019
    Posts:
    5
    for reference here's my existing code:


    Code (CSharp):
    1.     public void CreateGUI()
    2.     {
    3.        
    4.         // Each editor window contains a root VisualElement object
    5.         VisualElement root = rootVisualElement;
    6.  
    7.         ObjectField field = CreateField();
    8.         root.Add(field);
    9.  
    10.         Button button = new Button();
    11.         button.text = "execute";
    12.         button.clickable.clicked += () => runToolCode(field);
    13.         VisualElement _button = button;
    14.         root.Add(_button);
    15.     }
    16.  
    17.     public void runToolCode(ObjectField _field)
    18.     {
    19.         AnimationClip value = (AnimationClip)_field.value;
    20.  
    21.         if (value.GetType() == typeof(AnimationClip))
    22.         {
    23.             UpdateAnimations.UpdateLoop(value);
    24.         }
    25.         else { Debug.Log("didn't work"); }
    26.            
    27.     }
    28.     public ObjectField CreateField()
    29.     {
    30.         ObjectField _field = new ObjectField();
    31.         _field.objectType = typeof(Animation);
    32.         _field.label = "animation";
    33.         _field.allowSceneObjects = false;
    34.         return _field;
    35.     }