using UnityEngine;
using System.Collections;
using Cinemachine;

public class CameraSizeFixer : MonoBehaviour
{
    public new Camera camera;

    float oldAspect;

    void Update() {
        if (Mathf.Abs(oldAspect - camera.aspect) > 0.01f) {
            FixCameraSize();
        }
    }

    void FixCameraSize()
    {
        oldAspect = camera.aspect;
        Vector2 resTarget = new Vector2(1920f, 1080f);
        Vector2 resViewport = new Vector2(Screen.width, Screen.height);
        Vector2 resNormalized = resTarget / resViewport; // target res in viewport space
        Vector2 size = resNormalized / Mathf.Max(resNormalized.x, resNormalized.y);
        camera.rect = new Rect(default, size) { center = new Vector2(0.5f, 0.5f) };
    }
}
