Search Unity

Struggling with AspectRatioFitter

Discussion in 'UGUI & TextMesh Pro' started by piginhat, May 25, 2017.

  1. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    I cannot for the life of me get this component to work and hope someone can help.

    Scenario:
    Load a photo from gallery on iOS and Android and have the aspect ration maintained in either orientation.
    Snap a photo from device camera on iOS and Android and have the aspect ration maintained in either orientation.
    This should work regardless of device screen resolution

    Problem:
    whatever options I set for mode and ratio I get the image either stretched in both orientations or works in one and not the other.

    I am supposed to update the aspect ratio at run time to adjust for the specific device it is running on?

    Been searching for a tutorial with no joy yet...
     
  2. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    I have found that if I set the AspectRatioFitter to Aspect Mode "Fit In Parent" and update the Aspect Ration in the Update function like so:

    Code (csharp):
    1.  
    2. aspectRatioFitter.aspectRatio = Camera.main.aspect;
    3.  
    This produces a correct photo orientation and aspect ratio on an iPad but not so on an iPhone 6+

    So this is my confusion with this component, why OK on one device but not on another?
     
    CrandellWS likes this.
  3. MoxorTheOne

    MoxorTheOne

    Joined:
    Jan 31, 2019
    Posts:
    2
    Late answer, but it could help someone else.

    Code (CSharp):
    1. private void CorrectAspectRatio(Image image)
    2.         {
    3.             var ratio = image.sprite.rect.width / image.sprite.rect.height;
    4.             image.GetComponent<AspectRatioFitter>().aspectRatio = ratio;
    5.         }
     
    CrandellWS likes this.
  4. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Hi I want to use the AspectRatioFitter but on a non UI non-canvas element.

    Is there a way to do that? Or is there one for non UI?
     
    Zywa likes this.
  5. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    178
    that code did not work for me, instead i had to access the `gameObject` attached to the image then get the component from that GameObect, just a minor fix

    Code (CSharp):
    1.        private void CorrectAspectRatio(Image image)
    2.         {
    3.             var ratio = image.sprite.rect.width / image.sprite.rect.height;
    4.             image.gameObject.GetComponent<AspectRatioFitter>().aspectRatio = ratio;
    5.         }
     
  6. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    178
    By chance The Camera aspect ratio matches one device and not the other... the code provided by @MoxorTheOne should be usable, which I updated with a minor fix in my previous post.

    I found putting the image as a child to a gameobject, and setting that gameobject to what ever the "MAX" size will be and then setting the `Aspect Ratio Fitter`.`Aspect Mode` to `fit in parent` in the child image gameobject (and upscaling image when need) works well for me

    but I set the actual aspect ratio using the image's original width and height
     
    Last edited: Apr 19, 2021