OneShotOneKill/Assets/Script/UGUI/Util/ZigzagGridLayout.cs

40 lines
1.0 KiB
C#
Raw Permalink Normal View History

2026-01-07 21:27:42 +00:00
using UnityEngine;
using UnityEngine.UI;
public class ZigzagGridLayout : GridLayoutGroup
{
public override void SetLayoutHorizontal()
{
base.SetLayoutHorizontal();
LayoutChildren();
}
public override void SetLayoutVertical()
{
base.SetLayoutVertical();
LayoutChildren();
}
private void LayoutChildren()
{
int cols = Mathf.FloorToInt((rectTransform.rect.width - padding.left - padding.right + spacing.x) / (cellSize.x + spacing.x));
for (int i = 0; i < rectChildren.Count; i++)
{
var child = rectChildren[i];
int row = i / cols;
int col = i % cols;
// 홀수 줄이면 col 순서를 반대로
if (row % 2 == 1)
col = cols - 1 - col;
float x = padding.left + col * (cellSize.x + spacing.x);
float y = padding.top + row * (cellSize.y + spacing.y);
SetChildAlongAxis(child, 0, x, cellSize.x);
SetChildAlongAxis(child, 1, y, cellSize.y);
}
}
}