'How do I get images to fit inside a panel in unity

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JeanLuc