Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Keep aspect ratio?

Discussion in 'Scripting' started by TylerPerry, Jan 27, 2013.

  1. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    I'm wondering how to keep an aspect ratio, IDK how to explain but like they say a picture is worth a thousand words(they are phones the black is the screen, and the green is the space that my game should use.) My games aspect ratio is 3:2:

     
  2. JFo

    JFo

    Joined:
    Dec 9, 2007
    Posts:
    217
    Use two cameras, background "LetterBox"-camera just to draw back background (set clear flags to Solid Color) and Main camera for actual rendering. Then set the main camera dimensions accordingly:

    Code (csharp):
    1.  
    2.  public void CalculateMainCameraDimensions()
    3.     {
    4.         // Maintain the 16:10 aspectratio using letterboxing (vertical or horisontal depending screen dimensions)
    5.        
    6.         if (LetterboxBgCamera.aspect < 1.6f)
    7.         {
    8.             MainCamera.rect = new Rect(0f, (1.0f - LetterboxBgCamera.aspect / 1.6f) / 2.0f, 1.0f, LetterboxBgCamera.aspect / 1.6f);
    9.         }
    10.         else
    11.         {
    12.             MainCamera.rect = new Rect((1.0f - 1.6f / LetterboxBgCamera.aspect) / 2.0f, 0, 1.6f / LetterboxBgCamera.aspect, 1.0f);
    13.         }
    14.          
    15.     }
    16.  
    You can call this every frame and test it live in editor by changing game view dimensions....

    BR,
    JFo
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  4. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577