Change one camera to be square.

Hello, so I have a system scripted and setup where after pressing the m key the camera switches from one to another. I need the second camera to be a square to be my game map, the only way I know how to do this is changing the games resolution. Anyone got any ideas on how to make one camera square with leaving the main camera as it is?

This will show you how to set up a cameras viewport and position on screen :

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Camera))]
public class ResizeCamera : MonoBehaviour {

    public enum CameraView {
        Free = 0,
        Square
    }

    [SerializeField]
    CameraView cameraView = CameraView.Square;
    [SerializeField]
    bool center = true;
    [SerializeField]
    [Range(0.0f, 1.0f)]
    float scale = 1.0f;
    [SerializeField]
    bool runOnlyOnce = false;

    // Internal
    float _cachedHeight = 0.0f;
    float _cachedWidth = 0.0f;

    void Start() {
        this.CheckScreenType();
    }

    void Update() {
        if(!this.runOnlyOnce) {
            this.CheckScreenType();
        }
    }

    void CheckScreenType() {
        switch(this.cameraView) {
        case CameraView.Square:
            this.SetSquare();
            break;
        case CameraView.Free:
        {
            this.camera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
        }
            break;
        default:
            break;
        }
    }

    /// <summary>
    /// Gets the size of the screen.
    /// </summary>
    void RefreshScreenSize() {
        this._cachedHeight = Screen.height;
        this._cachedWidth = Screen.width;
    }

    /// <summary>
    /// Sets the square.
    /// </summary>
    void SetSquare() {
        this.RefreshScreenSize();
        if(this._cachedHeight < this._cachedWidth) {
            float ratio = this._cachedHeight / this._cachedWidth;

            this.camera.rect = new Rect(this.camera.rect.x, this.camera.rect.y, ratio, 1.0f);

            if(this.center == true) {
                this.camera.rect = new Rect(((1.0f - ratio * this.scale) / 2), this.camera.rect.y * this.scale, this.camera.rect.width * this.scale, this.camera.rect.height * this.scale);
            }
        } else {
            float ratio = this._cachedWidth / this._cachedHeight;

            this.camera.rect = new Rect(this.camera.rect.x, this.camera.rect.y, 1.0f, ratio);

            if(this.center == true) {
                this.camera.rect = new Rect(this.camera.rect.x, (1.0f - ratio) / 2, this.camera.rect.width, this.camera.rect.height);
            }
        }
    }

    public void ScrictView(CameraView cameraView) {
        this.cameraView = cameraView;
    }
}

Place this on a camera and look at the inspector and play around with it. It should give you an idea as to how to do it, essentially what you want to do is calculate the ratio of the width to height then subtract it from 1.0f since the biggest screen size you can go is 1.0f. You can go higher than 1 but it will just mean the camera’s view is much beyond that of the default view. Let me know if you need any help.

1 Like