forked from TheCyaniteProject/FlexibleGridLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlexibleGridLayout.cs
73 lines (52 loc) · 2.15 KB
/
FlexibleGridLayout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FlexibleGridLayout : LayoutGroup
{
public enum FitType {
Uniform,
Width,
Height
}
public FitType fitType;
public int rows;
public int columns;
public Vector2 cellSize;
public Vector2 spacing;
public override void CalculateLayoutInputHorizontal() {
base.CalculateLayoutInputHorizontal();
float sqrRt = Mathf.Sqrt(rectChildren.Count);
rows = Mathf.CeilToInt(sqrRt);
columns = Mathf.CeilToInt(sqrRt);
if (fitType == FitType.Width)
{
rows = Mathf.CeilToInt(rectChildren.Count / (float)columns);
}
if (fitType == FitType.Height)
{
rows = Mathf.CeilToInt(rectChildren.Count / (float)rows);
}
float parentWidth = rectTransform.rect.width;
float parentHeight = rectTransform.rect.height;
float cellWidth = (parentWidth / (float)columns) - ((spacing.x / ((float)columns)) * (columns - 1)) - (padding.left / (float)columns) - (padding.right / (float)columns);
float cellHeight = (parentHeight / (float)rows) - ((spacing.y / ((float)rows)) * (rows - 1)) - (padding.top / (float)rows) - (padding.bottom / (float)rows);
cellSize.x = cellWidth;
cellSize.y = cellHeight;
int columnCount = 0;
int rowCount = 0;
for (int i = 0; i < rectChildren.Count; i++) {
rowCount = i / columns;
columnCount = i % columns;
var item = rectChildren[i];
var xPos = (cellSize.x * columnCount) + (spacing.x * columnCount) + padding.left;
var yPos = (cellSize.y * rowCount) + (spacing.y * rowCount) + padding.top;
SetChildAlongAxis(item, 0, xPos, cellSize.x);
SetChildAlongAxis(item, 1, yPos, cellSize.y);
}
}
public override void CalculateLayoutInputVertical() { }
public override void SetLayoutHorizontal() { }
public override void SetLayoutVertical() { }
}