I am trying to implement a ListView / Excel-style table layout by nesting horizontal layout groups inside vertical layout groups. So the hierarchy looks like this:
ListView (with Vertical layout group)
Row 1 (with Horizontal layout group)
Cell 1 (with Text component and Content Size Fitter)
Cell 2 (with Text component and Content Size Fitter)
Cell 3 (with Button and Content Size Fitter)
Row 2 (with Horizontal layout group)
Cell 1 (with Text component and Content Size Fitter)
Cell 2 (with Text component and Content Size Fitter)
Cell 3 (with Button and Content Size Fitter)
This mostly works. The problem I have is that the height of the Rows seems to come from the RectTransform height of the Row object - which is reasonable enough as a baseline, but I want the Row height to grow to contain its cells. (Similarly, the Row width doesnât grow to contain the Cells either, but since it doesnât have any sort of clipping mask, this is not a problem for me.)
A Content Size Fitter seems like the right answer, as it would be able to drive the Row height based on the Cell heights. But it gives me the dreaded yellow exclamation warning because Iâve put it inside a Layout Group. Presumably the system isnât intelligent enough to know that I want to drive the height with the Content Size Fitter and that the Layout Group wonât be doing this.
So, is there a proper workaround for this? Perhaps I need to create a new version of VerticalLayoutGroup that will respect dynamically sized contents and which will drive the row height?
Try checking out the âTable Layoutâ control in the UI Extensions project. May give you some better ideas for how to control this style of layout.
Link in my signature
If I understand you correctly you just need to âControl Child Sizeâ not by the layout group but by the children themselves. Easiest way to do this: Upgrade your project to Unity 5.5. There you have such an option.
I am talking about the option âControl Child Size (width / height)â which you can enable / disable directly below Child Alignment in Unity 5.5.
If this is not what you need, I apologize for not understanding your question correctly.
My understanding is that those options are for setting the size of the children objects relative to the parent. That is not what I need to do. I need to have the children objects change their size based on their own children.
Have you tried adding a content size fitter to Row 1 / Row 2 with Preferred Size enabled for Horizontal fit and where the width for âControl Child Sizeâ and âChild Force Expandâ is off? Should work.
Edit: Nevermind. I just realized you figured this out before but you donât like the warning of unity. I donât have a better approach
TO alter the size of a RectTransform relative to itâs children @Kylotan_1 you use the ContentSizeFitter (as you have in your example) on the parent control.
Child controls may need a LayoutElement to override or set the preferred / min width / heights as appropriate:
ListView (with Vertical layout group) (Content Size Fitter here would size to max/preferred dimensions of Row1âŚRow2)
Row 1 (with Horizontal layout group) â (Content Size Fitter here would size to max/preferred dimensions of Cell1âŚCell3)
Cell 1 (with Text component ) â (Content Size Fitter here would size to max/preferred dimensions of Text component)
Cell 2 (with Text component )
Cell 3 (with Button)
Row 2 (with Horizontal layout group) <-(Content Size Fitter here would size to max/preferred dimensions of Cell1âŚCell3)
Cell 1 (with Text component ) <-(Content Size Fitter here would size to max/preferred dimensions of Text component)
Cell 2 (with Text component)
Cell 3 (with Button)
I already explained this in the first post though - like I said, âA Content Size Fitter seems like the right answer, as it would be able to drive the Row height based on the Cell heights. But it gives me the dreaded yellow exclamation warning because Iâve put it inside a Layout Group.â
So either
a) Things are going to go wrong if I do that, or
b) Unity have had this incorrect warning for 2 years and donât seem interested in fixing it.
I only have a guess: b)
I used this setup several times already without having any issue (and I also didnât like the warning and was wondering if something could break⌠but it didnât break)