Reputation: 2854
I've got a ScrollView inside my Canvas which will be populated with a UI GameObject (Panel) at runtime. Because this dynamic child does not have a fixed size, I want to resize my ScrollView to the same width as the child. In other words: The ScrollView should be the width of it's child.
I think this is possible without the need of coding but I cannot get it to work properly.
I already tried to add a Content Size Fitter to the Content, Viewport and ScrollView, but this only results in a width of 0.
My structure is the following
Please tell me if you need some more information
it seems like the problem is with the GameObject Content. It does not resize with it's child element
Upvotes: 1
Views: 3097
Reputation: 5035
The only object that shouhld have ContentSizeFitter is the Content of the scrollview (only in the axis you want the content to expand). To work properly its best to also have a Vertical or Horizontal layout group, with checkboxes 'child control size' checked, and 'expand' un-checked. No all thats left to do is make sure all the children of the content object have LayoutElement with preferred layout sizes.
It can be a little bit tricky to get it right first few times but definitely possible. The order in which you add components is somewhat important : make sure you have layoutElements set up in the children first, then add the layout group, and only after it works correctly add the content size fitter. If you do it the other way around it tends to escape, as in you mnight get objects resized to 0 before you notice
Upvotes: 1