Klink45Join Date: 2011-06-06 Post Count: 26054 |
Every time I try to center a GUI sing formulas all over the Internet I get it wrong.
All I want is for my GUIs to stay put! Part of that is centering them. How do I make my frame be in the absolute middle of the screen, regardless of screen resolution?
u sicko! |
|
|
function centerGui(gui)
local absSize = gui.AbsoluteSize
gui.Position = UDim2.new(0.5, absSize.X/2, 0.5, absSize.Y/2)
end
Untested
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
|
Oops I made a few mistakes. Here's a cleaned up & fixed version.
#code
local function centerGui(gui)
local absoluteSize = gui.AbsoluteSize
gui.Position = UDim2.new(0.5, -(absoluteSize.X / 2), 0.5, -(absoluteSize.Y / 2))
end
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
@Remastered
That was nice of you! Thanks!
I also now know the formula... >:)
u sicko! |
|
|
No problem.
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
Is it possible to make that code work for frames inside of frames? I tried it in a frame inside a frame and it centered it in the frame but not the player's screen. I don't want to change the frame's parent because I have a lot of scripts referencing it.
u sicko! |
|
|
Hmm yea, give me a minute.
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
rip
u sicko! |
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
just set the position based on its parents position instead of itself
|
|
|
As in the formula, the basic idea of centering is a combination of offset and scale.
GUIs are positioned from their top left pixel. This means if you place it at
(.5, 0, .5, 0)
Then the top left pixel will be in the center of your screen. To get the middle of the GUI in the middle of the screen, you need to move the gui up to the halfway point of the gui and left to the halfway point of the gui. This is accomplished by getting the absolute size and dividing it by two, then using that (negated) as the offset value.
Pseudo formula:
(0.5, -absSize.X/2, .5, -absSize.Y/2)
Hopefully this explains what he did a little bit.
-The [Guy] |
|
|
Busy was spot on.
@Klink
Sorry, I tried and couldn't figure it out. What you can do is then scale the position down/up based on the parent frame's size.
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
THANK YOU BUSY
u sicko! |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
I tried this:
local absoluteSize = script.Parent.Parent.Parent.AbsoluteSize--The actual GUI
script.Parent.Position = UDim2.new(0.5, -(absoluteSize.X / 2), 0.5, -(absoluteSize.Y / 2))--The Frame
And it didn't center my frame. Am I doing something wrong?
u sicko! |
|
|
That will center it in whatever the parent of the frame is.
Do you want it centered in the scope of the whole screen?
-The [Guy] |
|
|
I spoke wrong - that will center the frame in the main GUI.
-The [Guy] |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
I want it centered in the whole screen, please.
u sicko! |
|
|
function ActualCenter(gui)
return UDim2.new(0.5-(gui.Size.X.Scale/2),
-gui.Size.X.Offset/2,
0.5-(gui.Size.Y.Scale/2),
-gui.Size.Y.Offset/2
)
end
While using Absolute is an option, it is always better to stay safe and rely on the actual element numbers. |
|
|
Btw parent must be a GuiBase, so most above solutions wouldnt work cause of this. Here's a better solution:
function CenterGuiBase(gui)
PortSize = workspace.CurrentCamera.ViewPortSize
pos = gui.AbsolutePosition
size = gui.AbsoluteSize
gui.Position = UDim2.new(((PortSize.X/2-size.X/2)/size.X*gui.Size.X.Scale),0,((PortSize.Y/2-size.Y/2)/size.Y*gui.Size.Y.Scale),0)
end
--[[ rip roblox inception ]]-- |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
@Speedy
Didn't work. My frame is inside a frame if that helps.
u sicko! |
|
Klink45Join Date: 2011-06-06 Post Count: 26054 |
@Lateral
Centers my frame inside of my frame, but thanks.
u sicko! |
|
|
Well I'm a complete idiot who doesn't know how porpotions work -.- let's try this again:
function CenterGuiBase(gui)
local PortSize = workspace.CurrentCamera.ViewportSize
local pos = {gui.AbsolutePosition}
local size = gui.AbsoluteSize
gui.Position = gui.Position + UDim2.new(.1,0,.1,0)
table.insert(pos,gui.AbsolutePosition)
gui.Position = UDim2.new(.1/(pos[2].X-pos[1].X)*(PortSize.X/2-size.X/2),0,.1/(pos[2].Y-pos[1].Y)*(PortSize.Y/2-size.Y/2),0)
end
--[[ rip roblox inception ]]-- |
|
|
Should work flawlessly.
--[[ rip roblox inception ]]-- |
|
|
Speedy your dumb, "the actual elements". Lmao.
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|
|
"el·e·ment
noun
a part or aspect of something abstract, especially one that is essential or characteristic."
Are you claiming that I was wrong in calling it the 'actual elements' instead of a screen region?
Feel free to try and correct it. I don't mind fixing my vocabulary but an interface object does sound like an element to a game. |
|
|
@Speedy
I'm disagreeing with the terminology; it's not necessarily wrong, just you should call them the actual 'properties' for clarity. Also; AbsolutePosition and AbsoluteSize are COMPLETELY reliable, you should not use the actual values as that can take a bit more memory because you have to do more math.
#code print("While I may look like a cataclysmic god of the eggs from the future, I am not.") |
|