Responsive UIGridLayout

  1. What do you want to achieve?
    Change CellSize on resize view port.

  2. What solutions have you tried so far?
    Did you look for solutions on the Developer Hub?
    I wrote a small script that detects changes in ViewportSize when inventory is open. Will this solution affect the performance?

local player = game.Players.LocalPlayer
local InventoryGui = player.PlayerGui.InventoryGui

script.Parent.MouseButton1Click:Connect(function()
	if not InventoryGui.Enabled then
		InventoryGui.Enabled = true

		workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(function()
			local width = workspace.CurrentCamera.ViewportSize.X

			if(width < 900) then
				for i, v in pairs(InventoryGui:GetDescendants()) do
					if v:IsA("UIGridLayout") then
						v.CellSize = UDim2.new(0.24, 0, 0.085, 0)
					end
				end
			else
				for i, v in pairs(InventoryGui:GetDescendants()) do
					if v:IsA("UIGridLayout") then
						v.CellSize = UDim2.new(0.18, 0, 0.08, 0)
					end
				end
			end
		end)
		
	else
		InventoryGui.Enabled = false
	end
end)

Thanks :slight_smile:

1 Like