Search Unity

Vertical list of images with different aspect ratios

Discussion in 'UGUI & TextMesh Pro' started by Tiollo, Apr 7, 2020.

  1. Tiollo

    Tiollo

    Joined:
    Mar 14, 2015
    Posts:
    23
    Hi!
    I'm trying to achieve something like the attached image.
    It's a list of dynamically loaded images that can have any aspect ratio and that are shown in one column layout, as if they were on a responsive website on mobile.
    The images should keep their original aspect ratio while resizing to fit 100% of the device width.
    By googling a little bit, I understood that I should use either the vertical layout group for the parent object or the "Aspect ratio fitter" for the children, but I can't find the right settings for this specific outcome.

    Any help? Thanks a lot!

    example1.jpg .
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    If you just want to display a sprite through an image (not a texture through a raw image) and do not need to have the boundaries of the Rect-Transform matching with the image size, you can do it very easily:
    • Create an Image.
    • Click on the anchor presets (left top thing in the Rect Transform), hold the Alt Key and select the lower right option.
    • Assign any dummy-sprite to the Source Image field.
    • Check "Preserve Aspect".
    If you need a Raw Image, you need to add an aspect ratio fitter to the game object set it to "Fit in Parent" and set the aspect via code (
    float aspectRatio = (float)texture.width / texture.height
    )