UDim2 fromOffset & fromScale to take Vector2 and Vector3 inputs

As a Roblox developer, it is currently too hard to create UDim2s from vectors
When doing math related to ui it’s generally easier to perform the calculations with vectors and to convert the end results into UDim2s because of the extra vector operation support, the ubiquity of vectors appearing in many other roblox apis and in my own code, and because I only ever use scale anyways (ex: when I want to read the x from a udim2 I have to do ud2.X.Scale instead of v2.X)

So Roblox recently added UDim2/fromOffset and UDim2/fromScale constructors. Which I was looking forward to being very useful but was disappointed to discover that they only take two numbers (x and y coordinates) as args. It would be very nice if they could also support passing vector2s and vector3s (with x and y components corresponding to ud2 x and y).

Maybe it’s worth mentioning that UDim2.new(x,0,y,0) is shorter than UDim2.fromScale(x,y)–and UDim2.new(x,0,y) is even shorter but probably bad practice. Would it be possible to rename the functions to something shorter?

10 Likes

What would be more useful is adding a :Components() method to all of the mentioned classes just like CFrame already has. That would pretty much solve the issue by not forcing us to use a temporary variable, though I am not against those methods accepting vectors.

The reason why you need to type .X.Scale instead of just .ScaleX is because a UDim2 contains 2 UDim objects which then contain Scale and Offset values, so it is more performant to do arithmetics on vectors anyways. UDim2s are meant to be used as a storage class, not something you would often need to change.

4 Likes

This feature would be really convenient since TextService:GetTextSize, CurrentCamera:WorldToScreen/ViewportPoint and AbsoluteSize/Position return Vector2s.

4 Likes

I need this.

I am working with many UI objects and the fact I have to assign the vector to a variable when accessing methods (like :GetTextBoundsAsync and :WorldToViewportPoint etc) and then index the vector’s properties to position/size a UI object is slowly but surely becoming a nuisance when this seems like such an (in my opinion) obvious thing to have.

Please add this. It would greatly speed up my workflow, and it doesn’t seem too out of proportion considering we can get data types passing other data types to constructors (think of constructors like CFrame.new(Vector3), BrickColor.new(Color3), etc).

1 Like