Home > Mobile >  How do I get images to fit inside a panel in unity
How do I get images to fit inside a panel in unity

Time:04-13

I have some UI panels laid out with layout groups and I wish to have images inside the panels. How do I go about doing this? I am aware that sprite masks exist but unsure if I can get the size of the panels to drive the bounding area of the mask. The panels are variable in size since I would like the UI to work on any aspect ratio so I can't just use a static object for the sprite mask.

My UI Layout

I would like these images to keep their original aspect ratio but be cropped to the size of the panels.

CodePudding user response:

On the panel gameobject you can add a RectMask2D component.

Make sure the image is a child of the panel and add the AspectRatioFitter with a AspectMode "Envelope Parent" and the aspect ratio of the image.

  • Related