Advertisement
Cakey3101

UIScaler

Apr 14th, 2025
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | Source Code | 0 0
  1. local collection_service = game:GetService("CollectionService")
  2. local GUI_service = game:GetService("GuiService")
  3. local workspace_service = game:GetService("Workspace")
  4.  
  5. local scale_component_to_base_resolution: {[UIScale]: Vector2} = {}
  6. -- Required property `AbsoluteContentSize` is not in `UILayout` superclass.
  7. type scrolling_frame_layout_component = UIGridStyleLayout | UIListLayout
  8. local layout_component_to_rescaling_connection: {[scrolling_frame_layout_component]: RBXScriptConnection?} = {}
  9.  
  10. local camera = workspace_service:FindFirstChild("Camera")
  11. local actual_viewport_size: Vector2? = nil
  12.  
  13. local has_initialized = false
  14.  
  15. local function rescale(scale_component: UIScale, base_resolution: Vector2)
  16.     assert(actual_viewport_size ~= nil)
  17.     scale_component.Scale = 1 / math.max(base_resolution.X / actual_viewport_size.X, base_resolution.Y / actual_viewport_size.Y)
  18. end
  19.  
  20. local function rescale_all()
  21.     local top_inset, bottom_inset = GUI_service:GetGuiInset()
  22.     actual_viewport_size = camera.ViewportSize - top_inset - bottom_inset
  23.  
  24.     for scale_component, base_resolution in scale_component_to_base_resolution do
  25.         rescale(scale_component, base_resolution)
  26.     end
  27. end
  28.  
  29. local function register_scale(component: UIScale)
  30.     local base_resolution = component:GetAttribute("base_resolution")
  31.     assert(typeof(base_resolution) == "Vector2")
  32.     scale_component_to_base_resolution[component] = base_resolution
  33. end
  34.  
  35. local function register_scrolling_frame_layout_component(layout_component: scrolling_frame_layout_component)
  36.     local scale_component_referral = layout_component:FindFirstChild("scale_component_referral")
  37.     assert(scale_component_referral:IsA("ObjectValue"))
  38.     assert(scale_component_referral.Value ~= nil)
  39.     assert(scale_component_referral.Value:IsA("UIScale"))
  40.     local scale_component: UIScale = scale_component_referral.Value
  41.     local scrolling_frame = layout_component.Parent
  42.     assert(scrolling_frame ~= nil)
  43.     assert(scrolling_frame:IsA("ScrollingFrame"))
  44.     -- https://devforum.roblox.com/t/automaticcanvassize-working-with-uilistlayout-and-uiscale-causes-wrong-automatic-size/1334861.
  45.     layout_component_to_rescaling_connection[layout_component] = layout_component:GetPropertyChangedSignal(
  46.         "AbsoluteContentSize"
  47.     ):Connect(function()
  48.         scrolling_frame.CanvasSize = UDim2.fromOffset(
  49.             layout_component.AbsoluteContentSize.X / scale_component.Scale,
  50.             layout_component.AbsoluteContentSize.Y / scale_component.Scale
  51.         )
  52.     end)
  53.     scrolling_frame.CanvasSize = UDim2.fromOffset(
  54.         layout_component.AbsoluteContentSize.X / scale_component.Scale,
  55.         layout_component.AbsoluteContentSize.Y / scale_component.Scale
  56.     )
  57.     print('set')
  58. end
  59.  
  60. local function initialize()
  61.     assert(has_initialized == false)
  62.     has_initialized = true
  63.  
  64.     collection_service:GetInstanceAddedSignal("scale_component"):Connect(function(object: Instance)
  65.         assert(object:IsA("UIScale"))
  66.  
  67.         register_scale(object)
  68.         rescale(object, scale_component_to_base_resolution[object])
  69.     end)
  70.     collection_service:GetInstanceRemovedSignal("scale_component"):Connect(function(object: Instance)
  71.         assert(object:IsA("UIScale"))
  72.         scale_component_to_base_resolution[object] = nil
  73.     end)
  74.  
  75.     for _, object in collection_service:GetTagged("scale_component") do
  76.         assert(object:IsA("UIScale"))
  77.  
  78.         register_scale(object)
  79.     end
  80.  
  81.     collection_service:GetInstanceAddedSignal("scrolling_frame_layout_component"):Connect(function(object: Instance)
  82.         assert(object:IsA("UIGridStyleLayout") or object:IsA("UIListLayout"))
  83.         register_scrolling_frame_layout_component(object)
  84.     end)
  85.     for _, object in collection_service:GetTagged("scrolling_frame_layout_component") do
  86.         assert(object:IsA("UIGridStyleLayout") or object:IsA("UIListLayout"))
  87.         register_scrolling_frame_layout_component(object)
  88.     end
  89.     collection_service:GetInstanceRemovedSignal("scrolling_frame_layout_component"):Connect(function(object: Instance)
  90.         assert(object:IsA("UIGridStyleLayout") or object:IsA("UIListLayout"))
  91.         local rescaling_connection = layout_component_to_rescaling_connection[object]
  92.         assert(rescaling_connection ~= nil)
  93.         rescaling_connection:Disconnect()
  94.         layout_component_to_rescaling_connection[object] = nil
  95.     end)
  96.  
  97.     camera:GetPropertyChangedSignal("ViewportSize"):Connect(rescale_all)
  98.     rescale_all()
  99. end
  100.  
  101. initialize()
Tags: robloxstudio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement