Search Unity

How do you declare a generic typed control (e.g. PopupField) in UXML?

Discussion in 'UI Toolkit' started by mikevargas, Aug 18, 2019.

  1. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    22
    I want a drop-down control (similar to EnumField), but which allows for multi-selection of an arbitrary list. PopupField looks promising, but I don't see a way to declare it in UXML since it has generic type parameters. Obviously I couldn't do something like:

    <PopupField<string> ... />

    So what is the correct format for declaring this control? Or is this a base class and I'm supposed to be using one of its derived classes, and if so, which control would be best for allowing multi-select of a list of string values?
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    Unfortunately PopupField does not support creation from UXML. It is more intended to be used as base class for other types like TagField or LayerField. Also it doesn't support multi selection either.

    I can see two options for what you're trying to achieve.

    1) Use the ListView to display a scrollable list of elements. ListView supports multi-selection.
    From Unity use Window > UI > UIElements Samples and browse to the ListView to get a quick snippet.

    2) Register an event on any visual element and use the GenericMenu API from Unity to display a native OS menu. You can control individual selection states by using the
    on
    parameter of the
    AddItem()
    method.

    Hope this helps.
     
  3. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    22
    Hi,

    Thank you very much for the clarification. In the meantime I have created a generic MultiselectEnumField<T> (where T: Enum) which derives from MaskField and did some conversions behind the scenes to get it to allow filtering available values, converting values to mask values, etc. It seems to be working well so far.

    I will definitely check out the ListView as I'm sure I'll need that for my custom editor window, and the GenericMenu may be an excellent choice from which to base future custom controls.

    Thanks again.
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    All right.

    In 2019.3 we have added EnumFlagsField which could cover what your MultiselectEnumField fields does.
     
  5. eidetic

    eidetic

    Joined:
    Jun 27, 2016
    Posts:
    14
    I noticed this exists in the UnityEditor namespace. Sorry if this has been answered elsewhere, but is there a plan for moving this into the UnityEngine namespace so we can use EnumFlagsField + EnumField for in-game UI?
     
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    yes, there is a plan to have these fields work at runtime as well. No specific ETA though at this time.

    The main issue is that now these controls use the native OS dropdown menus which don't exist at runtime. But, that's a detail.