Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Ideas for scaling image

Discussion in 'Getting Started' started by tleylan, Jun 19, 2022.

  1. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    672
    I'm hoping someone here has thought about this (I assume someone has). I have a 3D plane object (not a UI) that I am using to display images on (one at a time). The images are loaded from a service and retrieving them and applying them via material.SetTexture is working fine. BUT the images are going to have a variety of proportions and whatever else it does I cannot have it stretching the image.

    So I think I need an algorithm (or solution) that determines an optimal size for the "image plane" to resize to within some limits. If the image was very wide (or tall) I don't want the display to expand to full size but rather to change proportionally so that any scaling of the image doesn't distort it.

    Any pointers or an article or anything?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You need to know the aspect ratio of your image. You do that by dividing its width into its height.

    Then you use that number to scale the size of your plane to that proportion (set the width to what you want and multiply that by the aspect ratio to get the height... Or maybe it's the other way around).
     
    tleylan likes this.
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    672
    That seems like a great idea. I seemingly could adjust for some max values in a dimension as well. I notice the MS Photos app does a good job of maintaining the "frame" part of the app and adjusting the image within it. This yields black borders on images that won't fit any other way which is perfectly acceptable as it keeps the picture frame size stable. Thanks.
     
    Schneider21 likes this.