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. Dismiss Notice

Vertical Layout Group

Discussion in 'UGUI & TextMesh Pro' started by topofsteel, Mar 6, 2015.

  1. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    I am having trouble controlling the size of elements added to a vertical layout group. If I add a button to a gameobject that has a vertical layout group, the button says 'Some values driven by vertical layout group'. I cannot control the width and height. But the only inputs the vertical layout group has are padding, alignment, force width and height. The force width and height maximize it there isn't a min, max or desired like on the grid layout group. How do I control the button size? Thanks.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    If you want to control the width and height of a child element of a layout group, then simply add a LayoutElement and set the Min / Preferred or flexible values for each axis.
    You will also have to uncheck the "Force child expand" option on the group itself
     
  3. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    So I would add that element to each (or the first one before I copy)? The problem I was having is the elements would stretch to fill the parent. I wanted to be able to add any number of buttons and have them space the same. I was able to achieve that with the grid, but it seemed like I should be able to with the Vertical Layout.

    In either case, how can I automatically size the height of the parent panel to match the number of elements? Thank you for your time.
     
    androidideas likes this.
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    You add it to each child (not the group)

    So
    • Group (Vertical Layout Group)
      • Child 1 (control + layout element)
      • Child 2 (control + layout element)
      • etc
    If you want to you can prefab the child to avoid recreating it every time.
    If you want the panel to only be as big as it's children, then also add a Content Size Fitter to the group, e.g.

    • Group (Vertical Layout Group + Content Size Fitter)
    • Child 1 (control + layout element)
    • Child 2 (control + layout element)
    • etc
    This will size the panel to how many children you have instead of the default which is the other way round (sizing children to fit the panel)
     
    Symons and matbest like this.
  5. matbest

    matbest

    Joined:
    Jan 17, 2017
    Posts:
    1
    This was the answer I was looking for! Make this more findable :)
     
    SimonDarksideJ likes this.
  6. Symons

    Symons

    Joined:
    Dec 1, 2015
    Posts:
    3
    Awesome, great help, thanks!