Hi
UIStroke is now live but the Thickness
property accepts a number instead of UDim
and with that it’s rather difficult to make it responsive.
If you apply a UIStroke to a button and set the Thickness
to 5 this is how it will look on a 1920x1080 resolution screen:
while it looks like this on the 667x375 screen
finally, this is how it looks like in studio
pretty big difference, we can make all of them look like the one in studio with some code though.
I’ve decided to include 2 methods - the first one is slightly less performant but automated and doesn’t need anything after you add the script, while the second one requires adding a tag to each UIStroke
instance you ever use
Method 1
for this method you just need to get the resolution of your current screen in studio, so we can calculate the exact thickness needed for other resolutions and make them look like the one in studio.
execute this in the command bar and check the output to get the resolution:
print(workspace.CurrentCamera.ViewportSize)
now add a LocalScript
inside StarterPlayerScripts
, paste the code, and make sure to change the STUDIO_SCREEN_SIZE
variable on the first line to the resolution that you got from the output.
Code
local STUDIO_SCREEN_SIZE = Vector2.new(0, 0) -- change 0, 0 to your studio resolution
local camera = workspace.CurrentCamera
local player = game:GetService('Players').LocalPlayer
local playerGui = player.PlayerGui
local function GetAverage(vector: Vector2): number
return (vector.X + vector.Y) / 2
end
local studioAverage = GetAverage(STUDIO_SCREEN_SIZE)
local currentScreenAverage = GetAverage(camera.ViewportSize)
local function AdjustThickness(ui: UIStroke)
local ratio = ui.Thickness / studioAverage
ui.Thickness = currentScreenAverage * ratio
end
local function ModifyUiStrokes()
local function ChangeGuiObject(instance: Instance)
if instance:IsA("UIStroke") then
AdjustThickness(instance)
end
end
for _, v in ipairs(playerGui:GetDescendants()) do
ChangeGuiObject(v)
end
playerGui.DescendantAdded:Connect(ChangeGuiObject)
end
ModifyUiStrokes()
Method 2
firstly, we’ll need to tag all UIStroke
instances so we won’t have to loop through the descendants later every time a UIStroke is added or the window gets resized.
execute this in the command bar to add a tag to each UIStroke
instance in your StarterGui
for _, v in ipairs(game.StarterGui:GetDescendants()) do if v:IsA("UIStroke") then game:GetService('CollectionService'):AddTag(v, 'UIStroke') end end
make sure you tag any other UIStroke
instances you add, you can use the Tag Editor plugin as well.
after that, you need to get the resolution of your current screen in studio, so we can calculate the exact thickness needed for other resolutions and make them look like the one in studio.
execute this in the command bar and check the output to get the resolution:
print(workspace.CurrentCamera.ViewportSize)
finally, add a LocalScript
inside StarterPlayerScripts
, paste the code, and make sure to change the STUDIO_SCREEN_SIZE
variable on line 3 to the resolution that you got from the output.
Code
local CollectionService = game:GetService('CollectionService')
local STUDIO_SCREEN_SIZE = Vector2.new(0, 0) -- change 0, 0 to your studio resolution
local camera = workspace.CurrentCamera
local instanceAddedSignal = nil -- stores a connection
local function GetAverage(vector: Vector2): number
return (vector.X + vector.Y) / 2
end
local studioAverage = GetAverage(STUDIO_SCREEN_SIZE)
local currentScreenAverage = GetAverage(camera.ViewportSize)
local function AdjustThickness(ui: UIStroke)
local ratio = ui.Thickness / studioAverage
ui.Thickness = currentScreenAverage * ratio
end
local function ModifyUiStrokes()
currentScreenAverage = GetAverage(camera.ViewportSize) -- re-calculate the screen average as it could've changed
for _, ui: UIStroke in ipairs(CollectionService:GetTagged('UIStroke')) do
AdjustThickness(ui)
end
if instanceAddedSignal then
instanceAddedSignal:Disconnect()
end
instanceAddedSignal = CollectionService:GetInstanceAddedSignal('UIStroke'):Connect(AdjustThickness)
end
ModifyUiStrokes()
camera:GetPropertyChangedSignal('ViewportSize'):Connect(ModifyUiStrokes) -- this will fire every time a window size changes
after that, you can use the device emulator in the studio to confirm that they all look the same