Search Unity

Dropdown uxml control?

Discussion in 'UI Toolkit' started by Macro, Apr 30, 2020.

  1. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    I am just trying out some of the new ui elements/toolkit stuff and I cannot for the life of me find a dropdown control, I thought PopupField was what I needed, but cannot find that in the engine or editor xmlns. In code it seems to be part of the editor c# namespace but cant find any examples.

    Could someone let me know where it lives and bonus points for a quick example of how to use it?
     
  2. nateblessing

    nateblessing

    Joined:
    Apr 17, 2020
    Posts:
    10
    you want the choice fields. They are all in the UnityEditor name space and are in the Choice Fields foldout in the UI Builder.
    Code (CSharp):
    1.     // Choice Field Controls
    2.     private UnityEditor.UIElements.EnumField EnumFieldControl;
    3.     private UnityEditor.UIElements.TagField TagFieldControl;
    4.     private UnityEditor.UIElements.MaskField MaskFieldControl;
    5.     private UnityEditor.UIElements.LayerField LayerFieldControl;
    6.     private UnityEditor.UIElements.LayerMaskField LayerMaskFieldControl;
     
  3. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    Sorry to be a simpleton but it seems like none of these would be what I would be after (Assuming they all source their own data other than maybe the EnumFieldControl which I guess you give it an enum to be the options). I am after more of a generic dropdown where I can basically give it a dictionary/list of objects for its options (even if I have to run through progmatically and do it all in a custom object etc).

    The popupfield looked to be the one which I was after but as I say I cannot see that in the uxml side of things, as I think thats what we used in the imgui world.
     
  4. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    For anyone else who ends up here baffled how to get this all working I couldnt find a UXML way to add a popup field but you can add it progmatically like so:

    ```
    var someContainerElement = _uiRoot.Q<VisualElement>("#your-container-name");
    someContainerElement.Add(new PopupField<string>());
    ```

    However you have to set the list you want to use for options ahead of time via the constructor as I couldnt find any properties/methods that let you change it, so its often best if you build the popupfield before you need it with the right data, and then if that data is gonna change just have the list a member of the editor class or something.
     
    JoshDW and fuser like this.