Skip to content

Commit 87bc4ab

Browse files
committed
added FlexibleGridLayoutUtility
1 parent bd8654e commit 87bc4ab

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

UnityNativeToolkit/Assets/Scripts/Utils.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
public class FlexibleGridLayout : LayoutGroup
7+
{
8+
public enum FitType
9+
{
10+
Uniform,
11+
Width,
12+
Height,
13+
FixedRows,
14+
FixedColumns
15+
}
16+
public FitType fitType;
17+
18+
public int rows;
19+
public int columns;
20+
public Vector2 cellSize;
21+
public Vector2 spacing;
22+
23+
public bool fitX;
24+
public bool fitY;
25+
26+
public override void CalculateLayoutInputHorizontal()
27+
{
28+
base.CalculateLayoutInputHorizontal();
29+
30+
if(fitType == FitType.Width || fitType == FitType.Width || fitType == FitType.Uniform)
31+
{
32+
fitX = true;
33+
fitY = true;
34+
35+
float sqrRt = Mathf.Sqrt(transform.childCount);
36+
rows = Mathf.CeilToInt(sqrRt);
37+
columns = Mathf.CeilToInt(sqrRt);
38+
}
39+
40+
if(fitType == FitType.Width || fitType == FitType.FixedColumns)
41+
{
42+
rows = Mathf.CeilToInt(transform.childCount / (float)columns);
43+
}
44+
if (fitType == FitType.Height || fitType == FitType.FixedRows)
45+
{
46+
columns = Mathf.CeilToInt(transform.childCount / (float)rows);
47+
}
48+
49+
float parentWidth = rectTransform.rect.width;
50+
float parentHeight = rectTransform.rect.height;
51+
52+
float cellWidth = (parentWidth / (float)columns) - ((spacing.x / (float)columns) * (columns - 1)) - (padding.left / (float)columns) - (padding.right / (float)columns);
53+
float cellHeight = parentHeight / (float)rows - ((spacing.y / (float)rows) * (columns - 1)) - (padding.top / (float)rows) - (padding.bottom / (float)rows);
54+
55+
cellSize.x = fitX ? cellWidth : cellSize.x;
56+
cellSize.y = fitY ? cellHeight : cellSize.y;
57+
58+
int columnCount = 0;
59+
int rowCount = 0;
60+
61+
for (int i = 0; i < rectChildren.Count; i++)
62+
{
63+
rowCount = i / columns;
64+
columnCount = i % columns;
65+
66+
var item = rectChildren[i];
67+
68+
var xPos = (cellSize.x * columnCount) + (spacing.x * columnCount) + padding.left;
69+
var yPos = (cellSize.y * rowCount) + (spacing.y * columnCount) + padding.top;
70+
71+
SetChildAlongAxis(item, 0, xPos, cellSize.x);
72+
SetChildAlongAxis(item, 1, yPos, cellSize.y);
73+
}
74+
}
75+
76+
public override void CalculateLayoutInputVertical()
77+
{
78+
}
79+
80+
public override void SetLayoutHorizontal()
81+
{
82+
}
83+
84+
public override void SetLayoutVertical()
85+
{
86+
}
87+
}

UnityNativeToolkit/Assets/Scripts/Utils/FlexibleGridLayout.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)