Search Unity

Letterbox does not appear on mobile device

Discussion in '2D' started by cwicks, May 19, 2015.

  1. cwicks

    cwicks

    Joined:
    Apr 15, 2015
    Posts:
    1
    Hello, I'm trying to get letterboxing/pillarboxing to work in my game. It works fine in the unity editor, but when I build the game to Android the boxing does not appear, and some graphical artifacts are rendered in its place. These screen shots are from a test project that only contains an image and letter box code:

    Correct version in the Unity Editor:
    inUnityCapture.PNG

    Incorrect version on android device:
    inPhoneCapture.png
    (Notice the top and bottom sections of the screen)

    Code:
    // set the desired aspect ratio (the values in this example are
    // hard-coded for 16:9, but you could make them into public
    // variables instead so you can set them at design time)
    float targetaspect = ASPECT;

    // determine the game window's current aspect ratio
    float windowaspect = (float)Screen.width / (float)Screen.height;

    // current viewport height should be scaled by this amount
    float scaleheight = windowaspect / targetaspect;

    // obtain camera component so we can modify its viewport
    Camera camera = Camera.main;

    // if scaled height is less than current height, add letterbox
    if (scaleheight < 1.0f)
    {
    Rect rect = camera.rect;

    rect.width = 1.0f;
    rect.height = scaleheight;
    rect.x = 0;
    rect.y = (1.0f - scaleheight) / 2.0f;

    camera.rect = rect;
    }
    else // add pillarbox
    {
    float scalewidth = 1.0f / scaleheight;

    Rect rect = camera.rect;

    rect.width = scalewidth;
    rect.height = 1.0f;
    rect.x = (1.0f - scalewidth) / 2.0f;
    rect.y = 0;

    camera.rect = rect;
    }

    Any thoughts on this?
     
  2. ColossalPaul

    ColossalPaul

    Unity Technologies

    Joined:
    May 1, 2013
    Posts:
    174
    There won't be letterboxing in the player. The camera will expand/contract horizontally to account for the difference in screen aspect ratio. However, the ortho size will always be maintained. If you say 5, it will always be 5 world space units from center to top/bottom.