I’m making a plugin that sets all of the descendants of a screenGUI’s anchor point to 0.5,0.5 and convert their offset to scale.
However when I change the anchor point it also changes the position of the ui, is there some way to keep the same position while changing the anchor point?
Unfortunately Udim2s are ReadOnly so you have to change the position itself, and also, that logic won’t work. If the anchorpoint’s X was 0.5, it should go left exactly its X size halved many pixels, but your code will make it go right.
I was talking about what would happen if I set an anchorpoint. I think you confused Scale which when multiplied by the viewport size gives the actual position with Offset multiplied by the UIObject’s size.
Instead of this, you would do something like this:
local pos = Vector2.new(oldap.X - newap.X, oldap.Y - newap.Y) * sz / viewportsize
uiobj.Position += Udim2.fromScale(pos.X, pos.Y)
--Assuming viewportsize is defined, you can get that by doing workspace.Camera.ViewportSize
IMPORTANT: Only do that if you want to have the anchorpoint set accordingly to the studio’s camera (which is probably what you want to do but just wanted to say that)
By the way you can get the viewport size using this
This currently does not work, it just makes the ui object disappear. Also I’m not sure if using the workspace camera’s viewport size will work in all cases, as I have added support for nested objects(for example a frame as a child of a frame)
Code:
local oldAP = v.AbsolutePosition
v.AnchorPoint = Vector2.new(0.5,0.5)
local newAP = v.AbsolutePosition
local pos = Vector2.new(oldAP.X, newAP.X, oldAP.Y, newAP.Y) * v.AbsoluteSize / workspace.CurrentCamera.ViewportSize
v.Position += UDim2.fromScale(pos.X, pos.Y)
attempt to multiply a Vector2 with an incompatible value type or nil, maybe instead I should listen for when a gui object is added and setting the anchor point then instead?
you need to change v.AbsoluteSize to Udim2.fromOffset(v.AbsoluteSize.X, v.AbsoluteSize.Y) but I think you should first go back to the Scale solution and try this:
For example if we increase the anchorpoint, we will get a negative value from the summation and make it go towards the left (which is not what happens but yeah) so we should instead do subtraction
It works now! Still one final boss though, nested frames don’t have the proper position. I’m guessing this is related to the absolute size and position of the parent frame because I had to make these adjustments to get offset to scale to work for these.