Grid Layout Group with scalable content

Hello.

I have a UI panel that has GridLayoutGroup component. I will add to this panel child UI panels by code. Sometimes there will be 5 children, sometimes 9 and there will be more. When I add these children inside GridLayoutGroup, there will be some empty spaces or children will be outside of my panel.

How to make added children scaled so they fill all space inside GridLayoutGroup ?

I found a solution to this but it involves creating a new GridLayoutGroup class so it is a bit hacky.

It lets the grid position the elements normally, then uses the width of the grid to calculate the size the cells need to be to span the entire row.

[57985-stretchygrid.png*_|57985]

First grab the source of the original grid layout from Unity’s bitbucket [here][1] . Create a new script, pasting that code in and changing the name (to something like StretchyGridLayoutGroup.

And finally replace line 201:

SetChildAlongAxis(rectChildren_, 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);_

With this code:
float realsize = ((width - (spacing[0] * (actualCellCountX - 1))) / actualCellCountX);
SetChildAlongAxis(rectChildren_, 0, startOffset.x + (realsize + spacing[0]) * positionX, realsize);_
And now the children will regrow to fill the entire area of the grid. They regrow based on how the grid positioned the cells, so if the grid decided 1 cell per row, it would take up the whole row, but if the grid decides 2 per row then the 2 cells grow to fill. So first the regular layout code does it’s thing, then we grow our cells based on our width.
If this isn’t exactly what you are looking for, hopefully it at least helps!
_[1]: https://bitbucket.org/Unity-Technologies/ui/src/4f3cf8d16c1d8c6e681541a292855792e50b392e/UnityEngine.UI/UI/Core/Layout/GridLayoutGroup.cs?at=5.2&fileviewer=file-view-default*_
_

1 Like
  1. Add GridLayoutGroup as component of Panel.
  2. Add current script to the Panel as component.
  3. Attach your UI prefab (for example, InputField)

75690-gameunity-boxofideas-android-personal-opengl-41-20.png

public int rows;
public int cols;
public GameObject inputFieldPrefab;

void Start() {
	RectTransform parentRect = gameObject.GetComponent<RectTransform>();
	GridLayoutGroup gridLayout = gameObject.GetComponent<GridLayoutGroup>();
	gridLayout.cellSize = new Vector2(parentRect.rect.width / cols, parentRect.rect.height / rows);

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			GameObject inputField = Instantiate(inputFieldPrefab);
            inputField.transform.SetParent(gameObject.transform, false);
		}
	}
}

For us, we had to check and make sure all the children of the grid, had their scale to one, because the scale gets changed for some reason.
149980-untitled.png
Hopefully helps somebody!

In case anyone needs this:
This is solved in the UnityForums, here

The solution is this simple
,

   Vector2 YeniSize = new Vector2(549, 768);
        KartScreen.GetComponent<GridLayoutGroup>().cellSize = YeniSize;

thanks to this guy