├── FlexibleGridLayout.cs └── README.md /FlexibleGridLayout.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEngine.UI; 6 | 7 | public class FlexibleGridLayout : LayoutGroup 8 | { 9 | public enum FitType { 10 | Uniform, 11 | Width, 12 | Height 13 | } 14 | 15 | public FitType fitType; 16 | 17 | public int rows; 18 | public int columns; 19 | public Vector2 cellSize; 20 | public Vector2 spacing; 21 | 22 | 23 | public override void CalculateLayoutInputHorizontal() { 24 | 25 | base.CalculateLayoutInputHorizontal(); 26 | 27 | float sqrRt = Mathf.Sqrt(rectChildren.Count); 28 | rows = Mathf.CeilToInt(sqrRt); 29 | columns = Mathf.CeilToInt(sqrRt); 30 | 31 | if (fitType == FitType.Width) 32 | { 33 | rows = Mathf.CeilToInt(rectChildren.Count / (float)columns); 34 | } 35 | if (fitType == FitType.Height) 36 | { 37 | rows = Mathf.CeilToInt(rectChildren.Count / (float)rows); 38 | } 39 | 40 | float parentWidth = rectTransform.rect.width; 41 | float parentHeight = rectTransform.rect.height; 42 | 43 | float cellWidth = (parentWidth / (float)columns) - ((spacing.x / ((float)columns)) * (columns - 1)) - (padding.left / (float)columns) - (padding.right / (float)columns); 44 | float cellHeight = (parentHeight / (float)rows) - ((spacing.y / ((float)rows)) * (rows - 1)) - (padding.top / (float)rows) - (padding.bottom / (float)rows); 45 | 46 | cellSize.x = cellWidth; 47 | cellSize.y = cellHeight; 48 | 49 | int columnCount = 0; 50 | int rowCount = 0; 51 | 52 | for (int i = 0; i < rectChildren.Count; i++) { 53 | 54 | rowCount = i / columns; 55 | columnCount = i % columns; 56 | 57 | var item = rectChildren[i]; 58 | 59 | var xPos = (cellSize.x * columnCount) + (spacing.x * columnCount) + padding.left; 60 | var yPos = (cellSize.y * rowCount) + (spacing.y * rowCount) + padding.top; 61 | 62 | SetChildAlongAxis(item, 0, xPos, cellSize.x); 63 | SetChildAlongAxis(item, 1, yPos, cellSize.y); 64 | } 65 | 66 | } 67 | 68 | public override void CalculateLayoutInputVertical() { } 69 | 70 | public override void SetLayoutHorizontal() { } 71 | 72 | public override void SetLayoutVertical() { } 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FlexibleGridLayout 2 | An actually useable Grid Layout for Unity, as shown in this video: https://www.youtube.com/watch?v=CGsEJToeXmA 3 | 4 | This script is for a _flexable_ Grid Layout with auto-sizing shells. No more weird work-arounds for getting the default Grid Layout to fit all of your dynamically created objects! 5 | 6 | ### Old :S 7 | ![Annotation 2020-08-17 214535](https://user-images.githubusercontent.com/26806429/90465387-e4879200-e0d4-11ea-8dc6-ab029aef46e6.png) 8 | ![oldgridlayout](https://user-images.githubusercontent.com/26806429/90465255-9bcfd900-e0d4-11ea-8ec1-92461e747d02.gif) 9 | 10 | 11 | ### New~ 12 | ![Annotation 2020-08-17 214128](https://user-images.githubusercontent.com/26806429/90464317-a25d5100-e0d2-11ea-9e85-0bc8bfc699b1.png) 13 | ![newgridlayout](https://user-images.githubusercontent.com/26806429/90464760-9faf2b80-e0d3-11ea-8560-a903bf590a16.gif) 14 | --------------------------------------------------------------------------------