internal static void ScrollrectYFitGrid(ScrollRect scroll, GridLayoutGroup grid, float maxScrollRectY, bool fitScrollbar = true) { float gridSizeY = grid.transform.childCount * (grid.cellSize.y + grid.spacing.y) + grid.padding.top + grid.padding.bottom; float resHeight = Mathf.Min(maxScrollRectY, gridSizeY); scroll.GetRect().sizeDelta = new Vector2(scroll.GetRect().sizeDelta.x, resHeight); if (fitScrollbar) { scroll.vertical = !(maxScrollRectY > gridSizeY); } }