It says it all in the title, how do you detect screen size? Let’s say I was playing a game with a phone, and I wanted to know what the size of the phone is, how can I tell with a script? Or, is it possible to resize UI so it looks good on all platforms with constraints? I know I can use UI scale but I need to know the size of the screen first
The easiest way to do this (imo) is to use ScreenGui
’s AbsoluteSize
property.
Example Script
local size = ScreenGui.AbsoluteSize
local x = size.X
local y = size.Y
print("The user's screen size is", x, "by", y, "pixels.")
You could also use the property of the camera:
camera.ViewportSize
Current Vector2 size of the viewport.
But then, wouldn’t the UI look different on different devices? eg. a rectangular one on computer would look square on ipad. Assuming we are using percents
A ScreenGui object does not have a configurable size. It’s AbsoluteSize
value returns exactly how big the screen is (in pixels)
You could mess around with UI Constraints to create proper minimum and maximum sizes for each UI element.
This issue isn’t what you described in your title.
Nethertheless:
Use the offset
property, this defines the exact pixels of the element’s size, whereas scale
is the % of the screen the element will be sized.
frame.Position = UDim2.new(1,-frame.AbsoluteSize.X,0,0)
It is, I need the ui to be a size retaliative to the screen size, and I didn’t know how to detect screen size.
frame:GetPropertyChangedSignal("AbsoluteSize"):Connect(function(newSize)
frame.Position = UDim2.new(1,-newSize.X,0,0)
end)