Search Unity

[SOLVED] Function of "Child alignment" in "Vertical layout group"

Discussion in 'UGUI & TextMesh Pro' started by jeffrey08, Nov 4, 2015.

  1. jeffrey08

    jeffrey08

    Joined:
    Nov 4, 2015
    Posts:
    6
    Hiya!

    I'm totally new to Unity and probably doing something wrong. I'm trying to create a list to scroll through with a vertical layout group. The thing is, that when I create a new child for the group, the children are positioned in the wrong place. Instead of adding children to the bottom of the list, it's repositioning all the items in the list so they're centered at the top of the container.

    My container (which is a panel) has no specific components. The content of the container is anchored to the top left.

    My grid for the list (which is an image) has a "Content size fitter" and "Vertical layout group". For the fitter I've set the horizontal fit to "Unconstrained" and the vertical fit to "Preferred size". In the group I've set "Child alignment" to "Upper left" and I unchecked both options for "Child force expand". Again it's anchored to the top left.

    The buttons within the list have a "Layout element" component. I've checked "Preferred height" and given the original height and I've checked "Flexible width" to make it fit the width of the panel.

    Now I'm thinking that the alignment of the children should change when I change the "Child alignment" property, but sadly changing it does nothing.

    Am I doing something wrong or did I stumble on a bug?

    I'm using Unity version 5.2.2f1.

    Thanks in advance.

    All the best,

    Jeffrey
     
    RingoKam likes this.
  2. jeffrey08

    jeffrey08

    Joined:
    Nov 4, 2015
    Posts:
    6
    Apparently this was caused by a wrong value for the pivoting on the grid (the image I used). The default values were X = 0.5 and Y = 0.5, which I've changed to X = 0 and Y = 1.0 to fix the problem.
     
    xaldin-76, MM47, ppartida and 6 others like this.
  3. yotingo

    yotingo

    Joined:
    Jul 10, 2013
    Posts:
    44
    Thank you for posting this solution!
    I had been struggling with this for hours.
     
  4. JJJDon

    JJJDon

    Joined:
    May 4, 2018
    Posts:
    3
    Also helps me a lot.
     
  5. pengsuyu

    pengsuyu

    Joined:
    Aug 30, 2015
    Posts:
    1
    When you are using VerticalLayoutGroup or HorizontalLayoutGroup(inherited from HorizontalOrVerticalLayoutGroup) on a GameObject, you must check the recttransform.sizedelta (its width or height on Inspector). Because the calculation of the childs' positions depend on it. Once the width and height of a Gameobject is 0, the ChildAlignment of LayoutGroup is useless because the rect of Gameobject is shrinking to one point(0,0). And contentsizefitter will set the width and height of a recttransform to zero. So be careful to use the contentsizefitter. It's worthy spending a little time to read the source code of UGUI when you are struggling with these wired problem :)
     
    CrandellWS and useok2481 like this.
  6. metrixkita

    metrixkita

    Joined:
    Dec 31, 2020
    Posts:
    1
    Thank you!
     
  7. danceallnight

    danceallnight

    Joined:
    Aug 1, 2022
    Posts:
    2
    Thank you for sharing this!
     
  8. Koustav2010

    Koustav2010

    Joined:
    Nov 17, 2018
    Posts:
    2
    LayoutRebuilder.ForceRebuildLayoutImmediate(NPCQuestDetail.transform.Find("Body/ScrollArea/Content").GetComponent<RectTransform>());