protected void ResolveGridLayout()

in Azure Cloud Tutorials/Assets/MRTK/SDK/Features/UX/Scripts/Collections/GridObjectCollection.cs [330:445]


        protected void ResolveGridLayout(Vector3[] grid, LayoutOrder order)
        {
            int cellCounter = 0;
            int xMax, yMax;

            switch (order)
            {
                case LayoutOrder.RowThenColumn:
                    xMax = Columns;
                    yMax = Rows;
                    break;
                case LayoutOrder.ColumnThenRow:
                    xMax = Columns;
                    yMax = Rows;
                    break;
                case LayoutOrder.Vertical:
                    xMax = 1;
                    yMax = NodeList.Count;
                    break;
                case LayoutOrder.Horizontal:
                    xMax = NodeList.Count;
                    yMax = 1;
                    break;
                default:
                    xMax = Mathf.CeilToInt((float)NodeList.Count / rows);
                    yMax = rows;
                    break;
            }

            float startOffsetX = (xMax * 0.5f) * CellWidth;
            if (anchor == LayoutAnchor.BottomLeft || anchor == LayoutAnchor.UpperLeft || anchor == LayoutAnchor.MiddleLeft)
            {
                startOffsetX = anchorAlongAxis ? 0.5f * CellWidth : 0;
            }
            else if (anchor == LayoutAnchor.BottomRight || anchor == LayoutAnchor.UpperRight || anchor == LayoutAnchor.MiddleRight)
            {
                startOffsetX = anchorAlongAxis ? (xMax - 0.5f) * CellWidth : xMax * CellWidth;
            }

            float startOffsetY = (yMax * 0.5f) * CellHeight;
            if (anchor == LayoutAnchor.UpperLeft || anchor == LayoutAnchor.UpperCenter || anchor == LayoutAnchor.UpperRight)
            {
                startOffsetY = anchorAlongAxis ? 0.5f * CellHeight: 0;
            }
            else if (anchor == LayoutAnchor.BottomLeft || anchor == LayoutAnchor.BottomCenter || anchor == LayoutAnchor.BottomRight)
            {
                startOffsetY = anchorAlongAxis ? (yMax - 0.5f) * CellHeight : yMax * CellHeight;
            }
            float alignmentOffsetX = 0;
            float alignmentOffsetY = 0;

            if (layout == LayoutOrder.ColumnThenRow)
            {
                for (int y = 0; y < yMax; y++)
                {
                    for (int x = 0; x < xMax; x++)
                    {
                        if (y == yMax - 1)
                        {
                            switch (ColumnAlignment)
                            {
                                case LayoutHorizontalAlignment.Left:
                                    alignmentOffsetX = 0;
                                    break;
                                case LayoutHorizontalAlignment.Center:
                                    alignmentOffsetX = CellWidth *((xMax - (NodeList.Count % xMax)) % xMax) * 0.5f;
                                    break;
                                case LayoutHorizontalAlignment.Right:
                                    alignmentOffsetX = CellWidth * ((xMax - (NodeList.Count % xMax)) % xMax);
                                    break;
                            }
                        }

                        if (cellCounter < NodeList.Count)
                        {
                            grid[cellCounter].Set((-startOffsetX + (x * CellWidth) + HalfCell.x) + NodeList[cellCounter].Offset.x + alignmentOffsetX,
                                                 (startOffsetY - (y * CellHeight) - HalfCell.y) + NodeList[cellCounter].Offset.y + alignmentOffsetY,
                                                 0.0f);
                        }
                        cellCounter++;
                    }
                }
            }
            else
            {
                for (int x = 0; x < xMax; x++)
                {
                    for (int y = 0; y < yMax; y++)
                    {
                        if (x == xMax - 1)
                        {
                            switch (RowAlignment)
                            {
                                case LayoutVerticalAlignment.Top:
                                    alignmentOffsetY = 0;
                                    break;
                                case LayoutVerticalAlignment.Middle:
                                    alignmentOffsetY = -CellHeight * ((yMax - (NodeList.Count % yMax)) % yMax) * 0.5f;
                                    break;
                                case LayoutVerticalAlignment.Bottom:
                                    alignmentOffsetY = -CellHeight * ((yMax - (NodeList.Count % yMax)) % yMax);
                                    break;
                            }
                        }

                        if (cellCounter < NodeList.Count)
                        {
                            grid[cellCounter].Set((-startOffsetX + (x * CellWidth) + HalfCell.x) + NodeList[cellCounter].Offset.x + alignmentOffsetX,
                                                 (startOffsetY - (y * CellHeight) - HalfCell.y) + NodeList[cellCounter].Offset.y + alignmentOffsetY,
                                                 0.0f);
                        }
                    cellCounter++;
                    }
                }
            }
        }