Force letterbox?

I have what seems to be a common issue on the tubes, but a conclusive answer is hard to find so maybe the gurus here can help.

I’d like to force my game to 16:9 landscape on iOS and Android. Now forcing landscape is easily done in the build settings, and for UI Canvas I can achieve this easily. The problem I’m finding is with the camera.

On a 16:9 device, it’s obviously fine as the Canvas and camera are set up for 16:9. However on a 4:3 device (e.g. iPad) I would like the game to be letterboxed top and bottom (i.e. force the viewport to 16:9) but this seems to be hard to achieve.

One option I thought of is to have a black UICanvas “gobo” over the game to fake a letterbox, but there must be a better way… is there?

TIA

Actually I found a little script on Reddit that seems to do the trick when added to a Camera prefab. I’ll see if this has any unwanted effects down the track.

using UnityEngine;

public class CameraRatio : MonoBehaviour {
 
    void Start () {
        float targetaspect = 16.0f / 9.0f;
        float windowaspect = (float)Screen.width / (float)Screen.height;
        float scaleheight = windowaspect / targetaspect;

        Camera camera = GetComponent<Camera>();
       
        // 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 { // 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;
        }
    }

}
1 Like

tnx man :star_struck: :metal: