using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraAdjust : MonoBehaviour { public int logicalScreenWidth = 720; public int logicalScreenHeight = 1280; void Awake() { Camera camera = GetComponent(); float manualHeight; //然后得到当前屏幕的高宽比 和 你自定义需求的高宽比。通过判断他们的大小,来不同赋值 if (1.0f * Screen.height / Screen.width > 1.0f * logicalScreenHeight / logicalScreenWidth) { //如果屏幕的高宽比大于自定义的高宽比 。则通过公式 logicalScreenWidth * manualHeight = Screen.width * Screen.height; //来求得适应的manualHeight ,用它求出 实际高度与理想高度的比率 scale manualHeight = 1.0f * logicalScreenWidth / Screen.width * Screen.height; } else { //否则 直接给manualHeight 自定义的 logicalScreenHeight 的值,那么相机的fieldOfView就会原封不动 manualHeight = 1.0f * logicalScreenHeight; } float scale = manualHeight*1.0f / logicalScreenHeight; //Camera.fieldOfView 视野: 这是垂直视野:水平FOV取决于视口的宽高比,当相机是正交时fieldofView被忽略 //把实际高度与理想高度的比率scale乘加给Camera.fieldOfView。 //这样就能达到自动调节分辨率的效果 camera.fieldOfView *= scale; } }