I have a panel with GridLayoutGroup
on it.
Constraint is set to "Flexible" so it is not fixed in row and column and i don't want it to be.
Now I want get the column count for this GridLayout
in code behind, and then change the size of cells to fit on screen.
GridLayoutGroup layoutgroup;
I have the GridLayoutGroup variable in my code, but there is no function to get columns of it.
how do I do this?
Actually, you don't have to iterate through all the children. Finding the rightmost element of the first row will be enough.
Usage example:
var grid = GetComponent<GridLayoutGroup>();
Vector2Int size = grid.Size();
Helper class:
using System;
using UnityEngine;
using UnityEngine.UI;
public static class GridLayoutGroupHelper
{
public static Vector2Int Size(this GridLayoutGroup grid)
{
int itemsCount = grid.transform.childCount;
Vector2Int size = Vector2Int.zero;
if (itemsCount == 0)
return size;
switch (grid.constraint)
{
case GridLayoutGroup.Constraint.FixedColumnCount:
size.x = grid.constraintCount;
size.y = getAnotherAxisCount(itemsCount, size.x);
break;
case GridLayoutGroup.Constraint.FixedRowCount:
size.y = grid.constraintCount;
size.x = getAnotherAxisCount(itemsCount, size.y);
break;
case GridLayoutGroup.Constraint.Flexible:
size = flexibleSize(grid);
break;
default:
throw new ArgumentOutOfRangeException($"Unexpected constraint: {grid.constraint}");
}
return size;
}
private static Vector2Int flexibleSize(this GridLayoutGroup grid)
{
int itemsCount = grid.transform.childCount;
float prevX = float.NegativeInfinity;
int xCount = 0;
for (int i = 0; i < itemsCount; i++)
{
Vector2 pos = ((RectTransform)grid.transform.GetChild(i)).anchoredPosition;
if (pos.x <= prevX)
break;
prevX = pos.x;
xCount++;
}
int yCount = getAnotherAxisCount(itemsCount, xCount);
return new Vector2Int(xCount, yCount);
}
private static int getAnotherAxisCount(int totalCount, int axisCount)
{
return totalCount / axisCount + Mathf.Min(1, totalCount % axisCount);
}
}
You have to count them manually in a loop. You can get the column and row count from the GridLayoutGroup
component by doing the following:
1. Get the position of the first child of the Object under the GridLayoutGroup
object. The position should be from RectTransform.anchoredPosition
which returns Vector2
.
2. Loop from the second child to the rest of the child objects of the GridLayoutGroup
.
3. In each loop from #2, compare the x
component from the first child against the x
component from the current loop.
Create a boolean variable outside the loop that determines when to stop counting the row. The default value should be false
.
If the x
values match, it's a column, so increment column by 1
then set the boolean variable to true
.
If the x
values are not the same, and the boolean variable is false
, it's a row, so increment row by 1
.
Basically, you stop counting or incrementing row when column is found in the loop. That's what the boolean variable is used for.
void GetColumnAndRow(GridLayoutGroup glg, out int column, out int row)
{
column = 0;
row = 0;
if (glg.transform.childCount == 0)
return;
//Column and row are now 1
column = 1;
row = 1;
//Get the first child GameObject of the GridLayoutGroup
RectTransform firstChildObj = glg.transform.
GetChild(0).GetComponent<RectTransform>();
Vector2 firstChildPos = firstChildObj.anchoredPosition;
bool stopCountingRow = false;
//Loop through the rest of the child object
for (int i = 1; i < glg.transform.childCount; i++)
{
//Get the next child
RectTransform currentChildObj = glg.transform.
GetChild(i).GetComponent<RectTransform>();
Vector2 currentChildPos = currentChildObj.anchoredPosition;
//if first child.x == otherchild.x, it is a column, ele it's a row
if (firstChildPos.x == currentChildPos.x)
{
column++;
//Stop couting row once we find column
stopCountingRow = true;
}
else
{
if (!stopCountingRow)
row++;
}
}
}
Usage:
public GridLayoutGroup gridLG;
void Update()
{
int column = 0;
int row = 0;
GetColumnAndRow(gridLG, out column, out row);
Debug.Log("Column: " + column + " Row: " + row);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With