Make a 2D camera always fill the biggest square?

Hey all,

I am trying to get the main camera in my Unity project to always fill the biggest square it can in any resolution. That means it would have 2 equal pillar bars on a wider screen, but on a screen with a 1:1 ratio, it would fill the entire screen. How would I go about getting that effect?

Thanks

perspective or orthographic ?

In general get min of screen width & height. Use this min to set orthographic size (1/2) or calculate distance of camera (considering field of view)

It’s an orthographic camera, and what I’m trying to achieve is that the game, when in fullscreen, always reaches from the top to the bottom of the screen, but still is a square with a 1:1 ratio, and to have any extra space on either side of the camera be a pillar bar.

Untested but this should be in the right direction…you basically want to force a pillar box.

EDIT: You would not want to run this in Update though in a real game setup. You can add Execute in Editor Mode to see in unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScaler : MonoBehaviour {

    void Update ()
    {
      
        float screenAspect = (float) Screen.width / Screen.height;
        float adjustment;
      
        if (screenAspect > 1f)
        {
            adjustment = 1.0f - 1f/screenAspect;
            Camera.main.rect = new Rect(adjustment/2f, 0.0f, 1.0f-adjustment, 1.0f);
        }
    }
}