Advertisement
romanilyin

FlexibleGridLayout 2

Feb 14th, 2022
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.05 KB | None | 0 0
  1. //Tweaked the code to add more functionality
  2. //Added a minimum to some variables so they don't go under 0 or 1
  3. //Added all child alignments
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class FlexibleGridLayout : LayoutGroup {
  8.     public enum Alignment {
  9.         Horizontal,
  10.         Vertical
  11.     }
  12.     public enum FitType {
  13.         Uniform,
  14.         Width,
  15.         Height,
  16.         FixedRows,
  17.         FixedColumns,
  18.         FixedBoth
  19.     }
  20.    
  21.     public Alignment alignment;
  22.     [Space]
  23.     public FitType fitType;
  24.     [Min(1)]
  25.     public int columns;
  26.     [Min(1)]
  27.     public int rows;
  28.     [Space]
  29.     [Min(0)]
  30.     public Vector2 spacing;
  31.     public Vector2 cellSize;
  32.    
  33.     public bool fitX;
  34.     public bool fitY;
  35.  
  36.     public bool NudgeLastItemsOver;
  37.  
  38.     public override void CalculateLayoutInputVertical() {
  39.         base.CalculateLayoutInputHorizontal();
  40.         float sqrRt;
  41.  
  42.         switch (fitType) {
  43.             case FitType.Uniform:
  44.             default:
  45.                 fitX = fitY = true;
  46.                 sqrRt = Mathf.Sqrt(transform.childCount);
  47.                 rows = Mathf.CeilToInt(sqrRt);
  48.                 columns = Mathf.CeilToInt(sqrRt);
  49.                 rows = Mathf.CeilToInt(transform.childCount / (float)columns);
  50.                 columns = Mathf.CeilToInt(transform.childCount / (float)rows);
  51.                 break;
  52.             case FitType.Width:
  53.                 fitX = fitY = true;
  54.                 sqrRt = Mathf.Sqrt(transform.childCount);
  55.                 rows = Mathf.CeilToInt(sqrRt);
  56.                 columns = Mathf.CeilToInt(sqrRt);
  57.                 rows = Mathf.CeilToInt(transform.childCount / (float)columns);
  58.                 break;
  59.             case FitType.Height:
  60.                 fitX = fitY = true;
  61.                 sqrRt = Mathf.Sqrt(transform.childCount);
  62.                 rows = Mathf.CeilToInt(sqrRt);
  63.                 columns = Mathf.CeilToInt(sqrRt);
  64.                 columns = Mathf.CeilToInt(transform.childCount / (float)rows);
  65.                 break;
  66.             case FitType.FixedRows:
  67.                 fitX = fitY = false;
  68.                 columns = Mathf.CeilToInt(transform.childCount / (float)rows);
  69.                 break;
  70.             case FitType.FixedColumns:
  71.                 fitX = fitY = false;
  72.                 rows = Mathf.CeilToInt(transform.childCount / (float)columns);
  73.                 break;
  74.             case FitType.FixedBoth:
  75.                 fitX = fitY = false;
  76.                 break;
  77.         }
  78.  
  79.         float cellWidth;
  80.         float cellHeight;
  81.  
  82.         switch (alignment) {
  83.             case Alignment.Horizontal:
  84.                 cellWidth = (this.rectTransform.rect.width / (float)columns) - ((spacing.x / (float)columns) * (columns - 1)) - (padding.left / (float)columns) - (padding.right / (float)columns);
  85.                 cellHeight = (this.rectTransform.rect.height / (float)rows) - ((spacing.y / (float)rows) * (rows - 1)) - (padding.top / (float)rows) - (padding.bottom / (float)rows);
  86.                 break;
  87.             case Alignment.Vertical:
  88.             default:
  89.                 cellHeight = (this.rectTransform.rect.width / (float)columns) - ((spacing.x / (float)columns) * (columns - 1)) - (padding.left / (float)columns) - (padding.right / (float)columns);
  90.                 cellWidth = (this.rectTransform.rect.height / (float)rows) - ((spacing.y / (float)rows) * (rows - 1)) - (padding.top / (float)rows) - (padding.bottom / (float)rows);
  91.                 break;
  92.         }
  93.        
  94.         cellSize.x = fitX ? (cellWidth <= 0 ? cellSize.x : cellWidth) : cellSize.x;
  95.         cellSize.y = fitY ? (cellHeight <= 0 ? cellSize.y : cellHeight) : cellSize.y;
  96.  
  97.         int columnCount = 0;
  98.         int rowCount = 0;
  99.  
  100.         for (int i = 0; i < rectChildren.Count; i++) {
  101.             var item = rectChildren[i];
  102.             float xPos;
  103.             float yPos;
  104.             float xLastItemOffset = 0;
  105.            
  106.             switch (alignment) {
  107.                 case Alignment.Horizontal:
  108.                     rowCount = i / columns;
  109.                     columnCount = i % columns;
  110.                     if (NudgeLastItemsOver && rowCount == (rectChildren.Count / columns)) { xLastItemOffset = (cellSize.x + padding.left) / 2; }
  111.                     break;
  112.                 case Alignment.Vertical:
  113.                 default:
  114.                     rowCount = i / rows;
  115.                     columnCount = i % rows;
  116.                     if (NudgeLastItemsOver && rowCount == (rectChildren.Count / rows)) { xLastItemOffset = (cellSize.x + padding.left) / 2; }
  117.                     break;
  118.             }
  119.            
  120.             xPos = (cellSize.x * columnCount) + (spacing.x * columnCount) + padding.left + xLastItemOffset;
  121.             yPos = (cellSize.y * rowCount) + (spacing.y * rowCount) + padding.top;
  122.  
  123.             switch (m_ChildAlignment) {
  124.                 case TextAnchor.UpperLeft:
  125.                 default:
  126.                     //No need to change xPos;
  127.                     //No need to change yPos;
  128.                     break;
  129.                 case TextAnchor.UpperCenter:
  130.                     xPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.x + (spacing.x + padding.left + padding.left) - (columns * (cellSize.x + spacing.x + padding.left)))); //Center xPos
  131.                     //No need to change yPos;
  132.                     break;
  133.                 case TextAnchor.UpperRight:
  134.                     xPos = -xPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.x - cellSize.x; //Flip xPos to go bottom-up
  135.                     //No need to change yPos;
  136.                     break;
  137.                 case TextAnchor.MiddleLeft:
  138.                     //No need to change xPos;
  139.                     yPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.y + (spacing.y + padding.top + padding.top) - (rows * (cellSize.y + spacing.y + padding.top)))); //Center yPos
  140.                     break;
  141.                 case TextAnchor.MiddleCenter:
  142.                     xPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.x + (spacing.x + padding.left + padding.left) - (columns * (cellSize.x + spacing.x + padding.left)))); //Center xPos
  143.                     yPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.y + (spacing.y + padding.top + padding.top) - (rows * (cellSize.y + spacing.y + padding.top)))); //Center yPos
  144.                     break;
  145.                 case TextAnchor.MiddleRight:
  146.                     xPos = -xPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.x - cellSize.x; //Flip xPos to go bottom-up
  147.                     yPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.y + (spacing.y + padding.top + padding.top) - (rows * (cellSize.y + spacing.y + padding.top)))); //Center yPos
  148.                     break;
  149.                 case TextAnchor.LowerLeft:
  150.                     //No need to change xPos;
  151.                     yPos = -yPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.y - cellSize.y; //Flip yPos to go Right to Left
  152.                     break;
  153.                 case TextAnchor.LowerCenter:
  154.                     xPos += (0.5f * (this.gameObject.GetComponent<RectTransform>().sizeDelta.x + (spacing.x + padding.left + padding.left) - (columns * (cellSize.x + spacing.x + padding.left)))); //Center xPos
  155.                     yPos = -yPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.y - cellSize.y; //Flip yPos to go Right to Left
  156.                     break;
  157.                 case TextAnchor.LowerRight:
  158.                     xPos = -xPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.x - cellSize.x; //Flip xPos to go bottom-up
  159.                     yPos = -yPos + this.gameObject.GetComponent<RectTransform>().sizeDelta.y - cellSize.y; //Flip yPos to go Right to Left
  160.                     break;
  161.             }
  162.  
  163.             SetChildAlongAxis(item, 0, xPos, cellSize.x);
  164.             SetChildAlongAxis(item, 1, yPos, cellSize.y);
  165.         }
  166.  
  167.     }
  168.  
  169.     public override void SetLayoutHorizontal() {
  170.  
  171.     }
  172.  
  173.     public override void SetLayoutVertical() {
  174.  
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement