- Home /
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 guess you could replace the GridLayoutGroup with 9 HorizontalLayoutGroups nested inside a VerticalLayoutGroup, and set all of them to have child force expand on. That'll make the empty ones take up zero space, and the rest of them take up the remainder.
I'm pretty sure that the GridLayoutGroup is designed specifically to not resize it's cells based on the number that are filled.
O$$anonymous$$. I made as you suggested. Worked well.
Answer by taiku · Nov 12, 2015 at 01:52 AM
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.
First grab the source of the original grid layout from Unity's bitbucket here . Create a new script, pasting that code in and changing the name (to something like StretchyGridLayoutGroup.
And finally replace line 201:
SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);
With this code:
float realsize = ((width - (spacing[0] * (actualCellCountX - 1))) / actualCellCountX);
SetChildAlongAxis(rectChildren[i], 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!
Hello,I figure it out.Use this code.
float realsize = ((width - (spacing.x * (cellCountX - 1)) - padding.left - padding.right) / cellCountX);
Answer by ankad · Aug 08, 2016 at 07:12 PM
Add GridLayoutGroup as component of Panel.
Add current script to the Panel as component.
Attach your UI prefab (for example, InputField)
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);
}
}
}
Just wanted to say that I appreciate how much effort you put in to making this an excellent solution. I appreciate you!
Still works 2020.3.19f1 Needs a bit of changes depending of what you need. But still, it's damn efficient
Answer by TheGri108 · Dec 13, 2019 at 09:00 AM
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. Hopefully helps somebody!
This has been an issue so many times for me that I set the localscale = Vector3.one; as standard practice every time I dynamically add to a grid.
Answer by mfatihbarut · Mar 17, 2021 at 04:52 PM
The solution is this simple ,
Vector2 YeniSize = new Vector2(549, 768);
KartScreen.GetComponent<GridLayoutGroup>().cellSize = YeniSize;
thanks to this guy
Your answer
Follow this Question
Related Questions
Is there a way to use ScaleMode.Scaletofit on buttons? 0 Answers
How to scale a GUI (Lagacy) grid? 1 Answer
C# How to Drag and Scale with Mouse Window 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer