So I have been looking around to answer how to center a image button universally and I cannot get a good solution. Some people said to download the plugin AutoScaleLite and I tried that and it didn’t work. Others suggested some fiddling with AnchorPoint, which I don’t understand how to.
This is my code so far: pick it apart if you want.
local IB = script.Parent
local CSize = game.Workspace.CurrentCamera.ViewportSize
local CCenter = CSize.X / 2
IB.Position = UDim2.new({CCenter, 0.5}, {CCenter, 0.5})
IB.AnchorPoint = Vector2.new(CCenter, 0.5)
print(CCenter)
Have you checked the screengui’s enabled property is true? Also is your script parented to the IB object? Also, I think the problem with the positioning is that you’re still using offset.
When dealing with 2D player UI, you should literally never use offset because it will not reposition/resize depending on the screen. It doesn’t take screen size into account. So set the offsets to 0.
The documentation has a really good article on scale, offset, and anchor points. Explained really well. You should read this:
Either way, AnchorPoint should only be from 0-1, it uses percentages not pixels, for the anchor point to be in the center the X and Y should both be 0.5
You don’t need to calculate the camera size for this.
Roblox UI already provides an easy universal way to center UI elements using AnchorPoint + UDim2.
Why this works:
• AnchorPoint = (0.5, 0.5) makes the center of the UI object its reference point.
• Position = UDim2.new(0.5, 0, 0.5, 0) places that reference point in the middle of the screen.
• Works on all devices (PC, tablet, phone) without plugins or camera math.
If you want to center it inside a Frame instead of the screen, just place the button inside that Frame and use the same code.
Yeah thanks, I didn’t see this before hand. I wish this was mentioned somewhere in the main gui article but there ain’t no use complaining. Have a good day