Skip to content

Instantly share code, notes, and snippets.

@RoeiRubach
Created November 11, 2021 11:18
Show Gist options
  • Select an option

  • Save RoeiRubach/059c901d7b6e2891f0078de78582ade4 to your computer and use it in GitHub Desktop.

Select an option

Save RoeiRubach/059c901d7b6e2891f0078de78582ade4 to your computer and use it in GitHub Desktop.
[Flexible Grid Layout]
using UnityEngine;
using UnityEngine.UI;
public class FlexibleGridLayout : LayoutGroup
{
[SerializeField] private FlexibleGrid _grid;
public override void CalculateLayoutInputHorizontal()
{
base.CalculateLayoutInputHorizontal();
_grid.HandleFitTypes(transform);
// calculate how much space is able to work with
float parentWidth = rectTransform.rect.width;
float parentHeight = rectTransform.rect.height;
// define a size for each of our children, based off the info we now have
float cellWidth = (parentWidth / _grid.Columns) - GetRelativeReductionSpacing(_grid.Spacing.x, _grid.Columns) - (padding.left / (float)_grid.Columns) - (padding.right / (float)_grid.Columns);
float cellHeight = (parentHeight / _grid.Rows) - GetRelativeReductionSpacing(_grid.Spacing.y, _grid.Rows) - (padding.top / (float)_grid.Rows) - (padding.bottom / (float)_grid.Rows);
// assign values to our cell size
_grid.CellSize.x = _grid.FitByWidth ? cellWidth : _grid.CellSize.x;
_grid.CellSize.y = _grid.FitByHeight ? cellHeight : _grid.CellSize.y;
int columnCount = 0;
int rowCount = 0;
for (int i = 0; i < rectChildren.Count; i++)
{
rowCount = i / _grid.Columns;
columnCount = i % _grid.Columns;
var item = rectChildren[i];
var xPosition = (_grid.CellSize.x * columnCount) + (_grid.Spacing.x * columnCount) + padding.left;
var yPosition = (_grid.CellSize.y * rowCount) + (_grid.Spacing.y * rowCount) + padding.top;
SetChildAlongAxis(item, 0, xPosition, _grid.CellSize.x);
SetChildAlongAxis(item, 1, yPosition, _grid.CellSize.y);
}
}
private float GetRelativeReductionSpacing(float spacingVectorComponent, int gridVector)
{
return spacingVectorComponent / gridVector * gridVector - 1;
}
public override void CalculateLayoutInputVertical()
{
}
public override void SetLayoutHorizontal()
{
}
public override void SetLayoutVertical()
{
}
}
[System.Serializable]
public class FlexibleGrid
{
public enum FitTypes
{
Uniform,
Width,
Height,
FixedRows,
FixedColumns
}
public FitTypes FitType;
public bool FitByWidth;
public bool FitByHeight;
[Space]
[Min(1)] public int Rows;
[Min(1)] public int Columns;
[Min(0)] public Vector2 Spacing;
public Vector2 CellSize;
public void HandleFitTypes(Transform target)
{
if (FitType == FitTypes.Width || FitType == FitTypes.Height || FitType == FitTypes.Uniform)
{
FitByWidth = true;
FitByHeight = true;
CalculateTableSizeByChildCount(target);
}
if (FitType == FitTypes.Width || FitType == FitTypes.FixedColumns)
Rows = Mathf.CeilToInt(target.childCount / (float)Columns);
if (FitType == FitTypes.Height || FitType == FitTypes.FixedRows)
Columns = Mathf.CeilToInt(target.childCount / (float)Rows);
}
private void CalculateTableSizeByChildCount(Transform target)
{
float squareRoot = Mathf.Sqrt(target.childCount);
Rows = Mathf.CeilToInt(squareRoot);
Columns = Mathf.CeilToInt(squareRoot);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment