Unity2023.1.19贴段代码看看屏幕的适配问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCamara : MonoBehaviour
{
const float devHeight = 9.6f; // 开发设计高度
const float devWidth = 6.4f; // 开发设计宽度
// Start is called before the first frame update
void Start()
{
float screenHeight = Screen.height; // 屏幕高
Debug.Log("screenHeight = " + screenHeight);
//float screenWidth = Screen.width; // 屏幕宽
//Debug.Log("screenWidth = " + screenWidth);
float orthographicSize = this.GetComponent<Camera>().orthographicSize; //相机正交属性设置摄像机大小
float aspectRadio = Screen.width * 1.0f / screenHeight; // 屏幕宽高比 <1
Debug.Log("aspectRadio = " + aspectRadio);
float cameraWidth = orthographicSize * 2 * aspectRadio; // 相机宽 注:orthographicSize * 2刚好是摄像机正交大小的高即是相机高
Debug.Log("cameraWidth = " + cameraWidth);
if (cameraWidth < devHeight) // 相机宽度小于开发宽度时,调整摄像机正交属性大小
{
Debug.Log("cameraWidth < devHeight ");
orthographicSize = devWidth / (2 * aspectRadio);
Debug.Log("new orthographicSize = " + orthographicSize); // 摄像机正交属性的摄像机大小
this.GetComponent<Camera>().orthographicSize = orthographicSize;
}
}
// Update is called once per frame
void Update()
{
}
}
经测试会发现,摄像机的正交大小高度刚好是的orthographicSize两倍,宽度并不使用此算法。