adaptWidthToChildren on GUI.StackPanel only takes one child into account

Here’s the playground.

I also need one of the children to also be a StackPanel that will also adaptWidthToChildren, which should have size in pixels with padding, but the playground with it looks exactly the same.

You should not set adaptWidthToChildren = true as the stack panel is already adapting its width according to its children (when isVertical = false): just remove the line that sets this property.

1 Like

But it’s the same behaviour with a Rectangle.

Rectangles don’t reorganize their children, that’s what StackPanel is for. So, if you add two children without positionning them (with the left/top properties for eg), they will be one over the other in the rectangle.

Also, for containers other than StackPanel, the adaptWidthToChildren = true property only computes the width as the maximum width of the children, it does not take into account the left/top properties of those controls. Basically, if the root container has a single child it will work as expected, else it probably won’t. If as in your case you have two children, you should put them in a container and position them in this container and make this container a child of the root container:

1 Like