Maintaining aspect ratio of RectTransform when changing width and height

Hey there,

I’m trying to figure out if there’s an easy way to resize a RectTransform of an image while maintaining aspect ratio.

I’m working with Unity 2020.3.26f1.

If I use scale, I can definitely maintain the aspect ratio, but I understand it’s not the recommend way for working with sprites. So I’d like to continue using the width and height properties, but I can’t see a way to force a fixed aspect ratio while changing the width and height.

Say I have an RectTransform with a width of 485 and a height of 134, I basically need to use a calculator to maintain the aspect ratio as I can’t see any way to force it.

I do see the “Preserve Aspect” check box but it’s not working the way I’m expecting since I still have to increase the width and height parameters separately, guessing by how much it the size actually increased.

Thank you!

Check out the AspectRatioFitter component… I think that’s what you’re looking for.

That feels a little bit of an overkill for such a simple action, but I guess we’ll have to work with that. Thanks!