Search Unity

How to inherent content size?

Discussion in 'UIElements' started by TextusGames, Oct 6, 2019.

  1. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    146
    I am trying to autoexpand width of parent that has a child label component according to text width?
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    295
    Parent elements will by default accommodate child elements. The only time this doesn't work is if the parent element has a fixed width already applied, or a flex-grow/flex-shrink style.
     
  3. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    146
    I tested with new uiBuilder and was not able to achieve this.
     
  4. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    146
    Actually height of parent is inhereted from child.

    But how can parent inheret width of child?
     
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    295
    I think you're running into the fact that the root element (in the Builder's case, it's the Canvas element), which you have no control over, has Align >
    align-items: stretch;
    , by default. This makes children that have no explicit width stretch to take up the entire row. You can fix this by adding an extra VisualElement parent (essentially your own root), and setting its Align >
    align-items: flex-start;
    . See pic: upload_2019-10-7_18-13-29.png
    grandparent: blue -
    align-items: flex-start;

    parent: green
    child: red
    width: 200px;


    upload_2019-10-7_18-13-55.png
     
    TextusGames likes this.
  6. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    146
    Thanks the issue was the stretch alignment of parent.
    Now i undestand how it works.