Say you want ordinary flush left flow in Unity UI. Examples:
In fact, to answer THIS question I already achieved flush left flow "the hard way". Set out a "vertical group of rows" in Unity autolayout, attach FattieFlow at the top level,
public class FattieFlow : MonoBehaviour
{
public GameObject modelRow;
public GameObject modelItem;
public void Flow()
{
screen = GetComponent<RectTransform>().rect.width;
// move downwards any which need to be moved downwards
int row = 0;
while (row < transform.childCount) // (dynamic)
{
if (transform.GetChild(row).gameObject.activeSelf) FlowRow(row);
++row;
}
// et cetera....
}
}
FattieFlow
will completely re-flow it flush-left (by manipulating the lines, the hard way). Here's a script, demo, etc: the hard way.
But that's a poor solution.
Ideally, starting with UI.HorizontalLayoutGroup and UI.VerticalLayoutGroup it should be possible to create
FlowLayoutGroup
which lays out, flush left, into a block. (And indeed it should expand, and so on, the block as required ... exactly as HorizontalLayoutGroup
behaves).
It would seem that you'd have to start with HorizontalOrVerticalLayoutGroup
and work from there.
How would one do this (if it does not already exist)?
So far I have came up with this:
FlowLayoutGroup
How to Use
ContentSizeFitter
component to children and setHorizontal Fit
andVertical Fit
properties to Preferred SizeLayout Element
component to child and setPreferred Width
andPreferred Height
values. These values will control size of UI Element. You can also useMin Width
andMin Height
instead.This is how it looks like in inspector window :
Tested with buttons of different sizes and it works well.
NOTE :
GridLayoutGroup
class from Unity UI code to get desired behaviour. As it is derived fromLayoutGroup
which controls children'sRectTransform
properties. We need to useContentSizeFitter
andLayoutElement
on children to change their size.GridLayout
which allows to start vertical and start from other corners as well. I don't consider it a limitation as this is only behaviour that can be expected from a Flow Layout Group.Thanks!