直接上代碼 掛在相機上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraAuto : MonoBehaviour
{
public float camInitSize;// 初始化視口大小
public float screne_width;// 初始化寬
public float screne_height;//初始化高
private float auto_size;//實際視口大小
private float auto_width;// 實際寬
private float auto_height;//實際高
void Start()
{
auto_width = Screen.width;
auto_height = Screen.height;
//正交相機自適應寬 公式: 實際視口 = 初始化視口大小 * 初始寬高比 / 實際寬高比
auto_size = (camInitSize * (screne_width / screne_height)) / (auto_width / auto_height);
GetComponent<Camera>().orthographicSize = auto_size;
}
}