Search Unity

[Possible Bug] Layout Group not Recalculating when Gameobject is Disabled

Discussion in 'UGUI & TextMesh Pro' started by Pzula, Jan 27, 2017.

  1. Pzula

    Pzula

    Joined:
    Jan 25, 2013
    Posts:
    7
    Hey there,

    I'm having an issue with nested Vertical Layout Groups, Content Size Fitters, and dynamic content.

    Screenshot of UI with legend:



    Hierarchy:


    Main Container:
    • Vertical Layout Group Component
    • Padding of 10 and Spacing 10
    • Child Alignment: Middle Center
    • No Child Control Size enabled
    • No Child Force Expand enabled
    • Content Size Fitter Component
    • Preferred Size for Horizontal and Vertical
    Item Container:
    • Same setup as Main Container
    • Vertical Layout Group
    • No Child Control Size enabled
    • No Child Force Expand enabled
    • Content Size Fitter: Preferred Horizontal and Vertical
    Item:
    • Your standard Image UI
    • Rect width 200
    • Rect height 20

    Demo:

    Here is a video of me resizing and adding content to my UI. You can see everything works great expect at the end when I disable one of the Item gameobjects. The Item Container's rect transform recalculates itself and adjusts to the correct height, but the Main Container does not. The layout is not correctly calculated unless I disable and re-enable the Main Container's Vertical Layout Group.



    Is there a solution to this in a non-hacky fashion? Am I not using the layout components correctly? I do get a warning about having a Content Size Fitter on the Item Container, but this should be okay since the Main Container's Vertical Layout doesn't cause the child (Item Container) to expand, leaving the Item Container free to set its own Rect Transform values.

    Any help is much appreciated.

    Thanks

    Running Unity v5.5.0f3
     
  2. MMOERPG

    MMOERPG

    Joined:
    Mar 21, 2014
    Posts:
    50
    I'm not an expert at the new UI but this looks like a bug to me. A strange one at that. Since the Item Container resizes properly I would expect the Main Container to resize as well. Seems from this behavior that the Main Container(Content Fitters) does not check the size of its contents unless a child or descendant has been added, removed, or adjusted. Appears that unity coders add the check if a child gameObject is made active/inactive but forgot to include the check if any descendants (children of children) are made active/inactive. I would submit a bug report.
     
    Last edited: Jan 27, 2017
  3. Pzula

    Pzula

    Joined:
    Jan 25, 2013
    Posts:
    7
    Yeah this is probably the most likely explanation; I had a feeling it might be to do with the layout only checking one child-level deep, therefore missing changes made to children of children. I don't believe it's incorrect use of the UI Layouts because everything resizes and adjusts correctly with the exception of the demonstrated issue. Haven't found a clean solution for this problem yet. It does seem like a bug :(
     
  4. jdcampbe

    jdcampbe

    Joined:
    Jun 10, 2015
    Posts:
    8
    Did you ever find a solution? I've tried marking the parent as needing a rebuild and the object, but no luck.
     
  5. nikolay_i

    nikolay_i

    Joined:
    Jan 5, 2020
    Posts:
    13
    after the changes call
    Code (csharp):
    1.  
    2. void MethodWhichDisableObjects(){
    3.     ...
    4.     something.SetActive(false);
    5.     ...
    6.     StartCoroutine(DelayedRebuild());
    7. }
    8.  
    9. IEnumerator DelayedRebuild()
    10. {
    11.     yield return new WaitForEndOfFrame();
    12.     LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform) transform);
    13.     LayoutRebuilder.MarkLayoutForRebuild((RectTransform) transform);
    14. }
    15.  
     
  6. gresolio

    gresolio

    Joined:
    Jan 5, 2014
    Posts:
    17
    In short: it's not a Bug, but rather a feature by design ;), won't be fixed in current HorizontalOrVerticalLayoutGroup. To avoid using ForceRebuildLayoutImmediate or/and some manual update workarounds, there is another solution:

    Both "child controls size" checkboxes must be enabled and "child force expand" disabled for all ILayoutController (Horizontal, Vertical, also for nested ones), then to control the dimension of the objects you simply use the LayoutElement component (min, pref, flex sizes). In this case, any dynamic content is correctly aligned, regardless of the activation/deactivation/addition/removal of neighbors.

    Note 1: There is no need for ContentSizeFitter component for GameObjects, that are first level children of any LayoutGroup. The main thing is to correctly set the checkboxes, as described above. However ContentSizeFitter may be useful for top level container, or children of children that are not directly controlled by any LayoutGroup.

    Note 2: Minimize LayoutGroup nesting when possible. Layout rebuild is very expensive. More info on topic:
    Unite Europe 2017 - Squeezing Unity: Tips for raising performance.
     
    Last edited: Feb 4, 2020
    radektesar likes this.