Setting up the camera correctly (2D-Mode)

After some time of being absolutely okay with the normal screen/camera behavior with a single standard-camera I now realized, that the left and right edges of my playground (which has a static size) are cut off, if the screen has a too small width/height ratio.

It seems the default behavior of the camera is to grow wider as the screen has a bigger width/height ratio. But the other way around this means, that the camera can get really narrow if the ratio is really small.

What I want is, that if the screen deceeds a certain ratio the camera shall not get narrower anymore but grow higher to achieve the same ratio. That way it would be guaranteed, that my whole playground is always completely on the screen (sometimes with a padding above/below and sometimes with a padding on the left/right).

There is a function orthographicSize which can be found in the documentation. Using this your code should be the following:

	float newHeight = 4.8f;
	// If the camera is less than 8:4.8 (= 5:3) wide
	if (Screen.width / ((float) Screen.height) < 5f / 3f)
		newHeight = Screen.height / ((float) Screen.width) * 8; = newHeight/2;

Assuming the playground is 4.8 units high and 8 units wide.

First you need to capture the aspect ratio you author the app to (width / height)). If the aspect ratio on the device is greater (and therefore wider), you don’t do anything. If the aspect is less than the aspect you used in authoring, you need to adjust the orthographic size of the camera. A bit of untested code that I believe is right:

#pragma strict

private var authoredAspect = 1024.0 / 768.0;  // Width / height

function Start () {
	var currAspect : float = Screen.width / Screen.height;
	if (currAspect < authoredAspect) {
		Camera.main.orthographicSize = Camera.main.orthographicSize * authoredAspect / currAspect;