Search code examples
c#unity-game-engine

Get column and row of count from GridLayoutGroup programmatically


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.

enter image description here

 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?


Solution

  • 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);
    }