diff --git a/src/Runtime/GridLayoutGroup.cs b/src/Runtime/GridLayoutGroup.cs index 5d4ec9a..a8ef892 100644 --- a/src/Runtime/GridLayoutGroup.cs +++ b/src/Runtime/GridLayoutGroup.cs @@ -1,5 +1,4 @@ using UnityEngine; -using UnityEngine.UI; namespace Beardy { @@ -121,7 +120,10 @@ private void SetCellsAlongAxis(int axis) { int positionX; int positionY; - Vector2 cellStartOffset = (i+1 > rectChildrenCount - actualLastCellsCount) ? lastCellsStartOffset : startOffset; + bool isLastLine = i + 1 > rectChildrenCount - actualLastCellsCount; + Vector2 cellStartOffset = isLastLine ? lastCellsStartOffset : startOffset; + int thisCellCountX = isLastLine && startAxis == Axis.Horizontal ? actualLastCellsCount : actualCellCountX; + int thisCellCountY = isLastLine && startAxis == Axis.Vertical ? actualLastCellsCount : actualCellCountY; if (startAxis == Axis.Horizontal) { @@ -135,9 +137,9 @@ private void SetCellsAlongAxis(int axis) } if (cornerX == 1) - positionX = actualCellCountX - 1 - positionX; + positionX = thisCellCountX - 1 - positionX; if (cornerY == 1) - positionY = actualCellCountY - 1 - positionY; + positionY = thisCellCountY - 1 - positionY; SetChildAlongAxis(rectChildren[i], 0, cellStartOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]); SetChildAlongAxis(rectChildren[i], 1, cellStartOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);