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

Adapting aspect ratio iPhone to iPad

Discussion in 'iOS and tvOS' started by Thunder0ne, Feb 2, 2011.

  1. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
    Hi all,
    I am adapting a game for iPhone to iPad. I have used Viewport relative coordinates (0.0 - 1.0) and size for 2D objects, but, of course, the aspect ratio is different and therefore 2D objects appears a bit deformed on iPad.
    To fix this I am changing the Camera.rect property and leaving the less space possible not used by the camera. This works quite well besides the fact that the small areas of screen non used by the camera keep flashing with garbage - almost - random pixels.
    Any idea to remove this undesired effect of the Camera.rect property adapting?

    Here is the script I am using to adapt the viewport in order to keep original aspect ratio:
    Code (csharp):
    1. public class CameraScreenViewportResizer : MonoBehaviour
    2. {
    3.     public float _origScreenWidth = 480.0f;
    4.     public float _origScreenHeight = 320.0f;
    5.    
    6.     void Start()
    7.     {
    8.         float origScreenRatio = _origScreenWidth / _origScreenHeight;
    9.         float screenRatio = (float)Screen.width / (float)Screen.height;
    10.         Rect newViewportRect = new Rect();
    11.         float w = 0.0f;
    12.         float h = 0.0f;
    13.         if( screenRatio > origScreenRatio ){
    14.             w = (float)Screen.height * origScreenRatio;
    15.             newViewportRect.height = 1.0f;
    16.             newViewportRect.width = w / (float)Screen.width;
    17.             newViewportRect.y = 0.0f;
    18.             newViewportRect.x =  (1.0f - newViewportRect.width) / 2.0f;
    19.         }
    20.         else{
    21.             h = (float)Screen.width / origScreenRatio;
    22.             newViewportRect.width = 1.0f;
    23.             newViewportRect.height = h / (float)Screen.height;         
    24.             newViewportRect.x = 0.0f;
    25.             newViewportRect.y =  (1.0f - newViewportRect.height) / 2.0f;
    26.         }
    27.         for(int i=0; i < Camera.allCameras.Length; i++){
    28.             Camera cm = Camera.allCameras[i];
    29.             cm.rect = newViewportRect;
    30.         }
    31.     }
    32.    
    33. }
    Many thanks.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    What you need to do is have 2 cameras render with the fullscreen one rendering nothing but a mono colored backdrop for example and being the first to render, while the "real camera" does not clear the backdrop and renders as second one
     
  3. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
    Thanks a lot.
    Adding few comments just for reference for other people.

    The "black" camera is disabled, and its rendering is triggered by the main camera through Camera.Render() function. Also the "black camera" has the culling mask set to "nothing" in order to render nothing but the background.
     
  4. dejayc_legacy

    dejayc_legacy

    Joined:
    Oct 24, 2011
    Posts:
    17
    Hmmm, any impact on the frame rate?