Advertisement
Guest User

Dynamicly resizing grid layout for unity3d

a guest
Oct 19th, 2017
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. // Author: Matty Alan 2016
  5. public class DynamicGridLayoutGroup : GridLayoutGroup
  6. {
  7.     // Recalculates The Cell Size For The Grid Layout
  8.     // Currently; This forces the Grid Items To Be Square
  9.  
  10.     new void OnRectTransformDimensionsChange()
  11.     {
  12.         // Get The Total Panel Size.
  13.         float size = 0;
  14.         if (constraint == Constraint.FixedColumnCount)
  15.             size = rectTransform.rect.width;
  16.         else if (constraint == Constraint.FixedRowCount)
  17.             size = rectTransform.rect.height;
  18.  
  19.         // Divide That Size By The Item Count
  20.         size = size / (float)constraintCount;
  21.         cellSize = new Vector2(size, size);
  22.  
  23.         base.OnRectTransformDimensionsChange();
  24.     }
  25.  
  26.     public void ResizePanel(int itemCount)
  27.     {
  28.         var sizeDelta = rectTransform.sizeDelta;
  29.  
  30.         if (constraint == Constraint.FixedColumnCount)
  31.             sizeDelta.y = (cellSize.y * itemCount) / (float)constraintCount;
  32.         else if (constraint == Constraint.FixedRowCount)
  33.             sizeDelta.x = (cellSize.x * itemCount) / (float)constraintCount;
  34.  
  35.         rectTransform.sizeDelta = sizeDelta;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement