Content size fitter not working in child of a parent layout, how then?

I have a parent which i want to adjust to it’s children size.

Problem is one of the child’s has fixed size but the other will change.

So i thought:

  • Parent (Panel) - vertical layout group childs controls size + content size fitter min size
  • Dynamic Child (Text) - content size fitter preffered size
  • Static Child (Image) - no extra components

And this triggers an error that says that a Content size fitter component cannot be in a child of a parent that has a layout.

One workaround i found is to add a Layout Element component to the “dynamic” child and set it’s min width & min height to the text mesh pro preffered width & preffered height values.

But for that an extra script is required and it needs to be reasigned every time the text changes aswell.

There is no Unity’s way to do this?

No, I think there is no unity way to do it other than ignoring the warning (which actually works anyway in many situations).
But maybe you don’t need a layout group. Maybe you can achieve your goal with the right anchor settings.

1 Like

Content size fitter seems to not be doing anything alone at the parent.

I need it cause i want to use as minimum screen space as possible. It’s like a pop up panel which shows object info.

I understand why you need the content size fitter. But why do you need the layout group?

Unless i’m missing something the parent seems to need to fullfill all this criteria to scale to it’s children size:

  • A Layout Group component. (Vertical or Horizontal)
  • The option “child’s control size” unchecked.
  • A Content Size Fitter component.

If not it doesn’t seem to work at all.

Wouldn’t it not fit if you just add a content size fitter?
Usually you only need Layout Groups if you add children dynamically (via code)…