How can I auto-size a frame based on it's children?

I’m trying to make a UI to automatically scale down based on its children.

I looked through the whole forum but couldn’t find any good solutions for my problem.

For example: When there’s only one child in the frame, I want the UI to get smaller so it doesn’t leave any empty space.

From this:

image

To this:

image

2 Likes

I believe that this is something you’ll have to do with a script. There’s no way to change its size based on its children without a little bit of coding.

Yes, I know that but I put it here since it’s a part of game designing.

Also, how would I go on about doing this?

As I understand what you want to achieve, you could use the Automatic Size property.

If you don’t mean something like that, please explain

1 Like

There are many ways to do this, but the top two are: using UIListLayout.AbsoluteContentSize, and just going through the children and compiling its’ total size.

2 Likes

I have used Automatic Size but unfortunately, it doesn’t work how I want.

I want it to size the frame to the negative axis.

Can you explain the second way a bit further?

So let’s say you have a Parent Frame that has three Children. Child A has an AbsoluteSize of (200, 100), Child B has a size of (39, 68), and Child C has a size of (58, 198). To get the total size, you just need to add the sizes together.

function getAbsoluteSize(frame)
    local totalSize = Vector2.new()
    
    for _, Child in pairs(frame:GetChildren()) do
        if Child:IsA("GuiBase2d") then
            totalSize += Child.AbsoluteSize
        end
    end
    
   return totalSize
end
2 Likes

Very interesting. I’ll try to combine it with a loop to set the position at the same time so it doesn’t look out of place. Thank you!

1 Like