Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Layout Group and ContentSizeFitter in child

Discussion in 'UGUI & TextMesh Pro' started by kenny_cj, Jul 23, 2015.

  1. kenny_cj

    kenny_cj

    Joined:
    Feb 12, 2015
    Posts:
    4
    Hey everyone !

    So, Here is a graph of my UI :
    UIGraph.png .
    Basically, every Element is an expandable element and the list of elements is dynamic. I need my red RectTransform size to be dynamic because I want to be able to scroll vertically.
    I managed to make it work but Unity is warning me about the ContentSizeFitter that is attached to the Cyan gameobject telling me : "Parent has a type of layout group component. A child of a layout group should not have a Content Size Fitter component, since it should be driven by the layout group.".
    So am I doing something wrong ? Should my UI be designed in another way ?

    I really need those 2 Content Size Fitter components, otherwise the RectTransform size is not dynamic and I will not be able to scroll correctly.

    Thanks for your input / advice.
     
  2. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    We see exactly the same. Any advice would be great
     
  3. espresso

    espresso

    Joined:
    Nov 21, 2012
    Posts:
    1
    Same here, it seems to work fine but the warning makes me think I'm doing something wrong?

    In my case the size fitter is on the same object as the vertical layout group so there isn't even one on the 'parent'.
     
    WeiWuDe and rakkarage like this.
  4. w4der

    w4der

    Joined:
    Sep 17, 2011
    Posts:
    17
    Hi,
    Did you solve the problem?
    I would be grateful if you will explain how you fix this ;)
    Best Regards,
    W4der
     
  5. baerbel

    baerbel

    Joined:
    Feb 24, 2015
    Posts:
    5
    I would also love to know. Especially, because the same issue is present when following the Scrolling menus at runtime tutorial on the Unity web page around 12:25. The tutorial is relatively new, and Unity did not display a warning there. Now it does. Question is, how should we do it instead. In the tutorial he mentions writing a script that calculates the size. But this seems to be what content size fitter is already doing. Recreating an existing class, does not seem particularly useful. Any ideas?
     
  6. Chichkan

    Chichkan

    Joined:
    Jun 15, 2015
    Posts:
    5
    Bip901 likes this.
  7. baerbel

    baerbel

    Joined:
    Feb 24, 2015
    Posts:
    5
    @Chichkan thank you for trying to help. Unfortunately the page of the manual does not adress this particular issue, but it is helpful with regards to content size fitter anyways :)

    The problem seems to be that Unity sees the Scroll Rect script as a Layout group. I am not sure why as the documentation states it inherits from UIBehavior. What I ended up doing is putting the scroll rect script on the content panel itself and set the Viewport to the parent view that masks the list. This works and does not give a warning.
     
  8. Chichkan

    Chichkan

    Joined:
    Jun 15, 2015
    Posts:
    5
    If the problem is the warning message of Content Size Fitter on the panel with scrolling content, I think it's because one of the parents of your ScrollRect contains a LayoutGroup. If you don't want to see this warning, try to make the following hierarchy:

    ScrollRect
    --- Viewport (with Mask and Image)
    ------ Content (with Content Size Fitter and Vertical Layout Group)

    At least this is how new Unity 5.2.2 creates standart ScrollRect UI element and there is no warning messages.
     
    minhhieufly likes this.
  9. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    i had a similar problem where i had
    Parent with scroll rect, Mask & Image
    - child with horizontalLayout & content fitter
    --sub-children with layout element

    i found that it is caused by the scroll rect in the parent (the warning disappears if you remove it).
     
    nirvanajie and ghulamabbas786 like this.
  10. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    wtf... this is counter intuitive, most of the time you will want a scroll rect game object to be parent of a game object with a content size fitter since you never know what it's going to be the size of the content rect... I just nested the game object with content size fitter into an empty game object and nested this empty game object into the scroll rect game objct and that worked, but this makes totally no sense.
     
  11. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    yeh, i don't think it should show this warning, however, the warning doesn't result in any buggy behavior (as far as i've tested), so i think you can keep the scroll rect without problems.
     
  12. chilton

    chilton

    Joined:
    May 6, 2008
    Posts:
    561
    Holy cow this is still happening. Oh sure I can get around it by nesting a game object between the layers, but that's a pretty hacky solution.
     
  13. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Yeap, this is still happening in 5.5
     
    Jesper-Nielsen likes this.
  14. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    I guess the problem is that if you force expand vertically in the vertical layout group while at the same time trying to fit vertically in the content size fitter too then you have a conflict. There doesn't seem to be one if layout group is not set to touch the same direction as the content size fitter. Still it's not nice to see a warning:(
     
    Zamaroht likes this.
  15. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Well.. but this is what you need to do for scrollrect ro work. It is even shown in the official unity tutorial video... but the video doesnt show the warning.
     
    Jesper-Nielsen likes this.
  16. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    Exactly my point actually. I wasted 2 hours yesterday trying to get a simple chat box working - read this HOWTO and tried following it: https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
    Still those warnings kept showing up and I thought it was me doing something wrong.
    The warning system really ought to be a little more intelligent and only show a warning if there is an actual conflict, instead of the general one.
    A little more googling sent me to this thread.

    Most of the time Unity is a really nice engine but sometimes you just want to pull your hair out because it forces you to jump through hoops of peculiar and scarcely documented behaviour.
     
    mimiCryMuffin likes this.
  17. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Yeah i agree.

    I do give kudos to the guys at unity for trying hard. I am sure they are doing as best as they can but i cant get over this feeling that they are being a bit over run and stretched thin most of the time from sheer amount of support needs.
     
    mimiCryMuffin and Jesper-Nielsen like this.
  18. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
  19. taylorjamess91

    taylorjamess91

    Joined:
    Jul 30, 2017
    Posts:
    1
    Finally solved this after...way too much time. There's a pretty painless solution - although it was NOT obvious and took a lot stabbing around in the dark. My problem was similar to the original post. I'm creating an app where there are Questions posted, and every question will have a variable number of answers displayed underneath. (I know there aren't going to be an insane number of answers for each question, so I want all of the questions and answers to be visible, and I want all of it to be scrollable.) Every question will have a different number of answers, so it's important that every Q/A parent object have its own perfectly fitted rect transform size, so that the spacing between each Q&A cluster is consistent. Here's what I eventually did to make sure each q/a cluster is dynamically fitted, no matter how many or few answers 1.) Create new UnityUI ScrollView. 2.) DropDown the arrow for Viewport, and delete "Content" object underneath of it; then replace it with a holder object that you want to contain everything that is scrollable (I named mine "AllQuestionsAllAnswers"). 3.) AllQuestionsAndAllAnswers has a ContentSizeFitter component and a vertical layout group component. 4.) As a child of AllQuestionAndAllAnswers I have several prefabs representing individual Q&AClusters (I call these objects PromptAndContent). Each PromptAndContent object has a vertical layout group, and content size fitter component attached. On the vertical layout component, I need to use child force expand for both height and width to get a nice consistent spacing between Q&A block. For the Content Size fitter, in the Vertical Fit dropdown, I have to use ***Preferred Size***. (This makes all the difference. Before I was using Min Size and I would get the error discussed above, and it would not work at all.) Now, after using Preferred Size, I still see the warning, but when I add new Answer prefabs dynamically, as children under PromptAndContent, I see that the bounding box for PromptAndContent object adjusts to fit all answers perfectly. Let me know if it works for you.


    prefSize.png
     
    nithinsb, Zamaroht, seloran and 4 others like this.
  20. TharosTheDragon

    TharosTheDragon

    Joined:
    Sep 2, 2017
    Posts:
    13
    So the warning is just another Unity bug?
     
    dman8723, Zamaroht, seloran and 2 others like this.
  21. iamkomal

    iamkomal

    Joined:
    Jan 15, 2014
    Posts:
    1
    I have already done with these steps still I am able to scroll text forever. However, my text element is child of last component i.e. sub children with layout element.
    May you help me out with this.
     
    Sejfi likes this.
  22. Ni-Donoso

    Ni-Donoso

    Joined:
    Jun 24, 2019
    Posts:
    2
    Hello Everyone
    I had a same problem but I used this method.

    content (layout & content fitter)
    - child layout1(layout & content fitter)
    - child layout2(layout & content fitter)
    ...
    - child layoutn(layout & content fitter)

    Code:
    Code (CSharp):
    1. int refreshCounter = 0;
    2.  
    3. void Update()
    4. {
    5.     if (refreshCounter > 0)
    6.     {
    7.         refreshCounter --;
    8.  
    9.         if (refreshCounter == 0)
    10.         {
    11.             content .GetComponent<Image>().enabled = !content .GetComponent<Image>().enabled;
    12.         }
    13.     }
    14. }
    15.  
    16. void ChildProcess()
    17. {
    18.     //add or remove elements to or from child layout1~n
    19.     ...
    20.  
    21.     refreshCounter = 2;
    22.     //not 1, because after next first frame, the canvas is rendered
    23. }
    @kenny_cj @Bravo_cr @espresso @w4der
     
    Last edited: Jul 17, 2019
  23. -JoSeM-

    -JoSeM-

    Joined:
    Jul 8, 2014
    Posts:
    9
    This helped me, I found the way to solve the problem also. My scheme is like this
    Scroll View
    Content (vertical layout & content size fitter (vertical fit = preferred size))
    Text (content size fitter (vertical fit = unconstrained))

    By script I am changing the text, but I have to change by script the Text Content size fitter vertical fit
    GetComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
    GetComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.Unconstrained;

    By doing this script switch, the text is resized correctly
     
  24. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Have a vertical layout group( not controlling child size height not child force expand height) featuring items with vertical groups ( vertical child size off, force expand vertical on) each complimented with a content size fitter with vertical fit set to preferred.

    Bye,

    Jean
     
  25. LT23Live

    LT23Live

    Joined:
    Jul 8, 2014
    Posts:
    83
    After following this advice I landed on this link.
    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UIAutoLayout.html

    This was another link inside of the tutorial quoted above. It explains in details to make everything expand the way you need it too but It does not work if your contents hierarchy is more than 2 deep unfortunately.

    In that same link I found the "Triggering Layout Rebuild" section to be helpful for exactly what you are trying to do.
     
  26. Pepillou

    Pepillou

    Joined:
    May 1, 2013
    Posts:
    1
    I got these results without warnings:
    layouts.png

    This was the configuration

    Code (CSharp):
    1. Scroll View
    2. --Viewport
    3. ----Content
    4.     [Content Size Fitter]
    5.       Vertical Fit: Preferred Size
    6.     [Vertical Layout Group]
    7.       Control Child Size: W/H
    8.       Child Force Expand: W/H
    9. ------Prefab
    10.       [Vertical Layout Group]
    11.         Control Child Size: W/H
    12.         Child Force Expand: W/H
    13. --------TMP
    14. --------Image
    15.           [Layout Element]
    16.             Min Height:20
    17. --------TMP
    18. --------TMP
    19. --------TMP
     
  27. MsRedNebula

    MsRedNebula

    Joined:
    May 14, 2019
    Posts:
    4
    That worked for me - thank you so much! I've been wrestling with this problem for ages.
     
  28. White_Wabbit

    White_Wabbit

    Joined:
    Aug 16, 2019
    Posts:
    10
  29. FeniXD

    FeniXD

    Joined:
    May 16, 2017
    Posts:
    9
    Spent weeks trying to work this out, and even moved over to UI Toolkit to try that out (Then realised it's half done and lacking most of the features we need). This solution works great. Thank you!