Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to auto-size an EnumField based on its contents?

Discussion in 'UIElements' started by mikevargas, Sep 16, 2019.

  1. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    14
    Hi,

    I'm hoping that an explanation of how to do this will illuminate solutions to similar problems as well. If I create a plain EditorWindow and add an EnumField to the root visual element, its width will fill the entire screen regardless of the length of the values of the EnumField.

    I would like the width of the field control to be auto-sized to fit the field values--even if it's always set as the maximum width of any of the values.

    How would I do this?
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    61
    Hi,

    This is the default behavior of flexbox. The root VisualElement flex-direction is column and by default its children will stretch on the cross-axis (horizontal) to fill all the space.

    To change this you can use "align-self: flex-start" on the EnumField or "align-items: flex-start" on the EnumField parent which is the root VisualElement in this case.

    See this page for more information about the layout : https://docs.unity3d.com/Manual/UIE-LayoutEngine.html