目录
根据设备分辨率动态设置相机 orthographicSize
但是导出apk安装到手机上的时候 我的手机分辨率是1080*2440
10*(Screen.Width/Screen.height)
所以需要按照开发的尺寸(原先的宽)度来重新计算size ,挂在摄像机上
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class CameraAdaper : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
- SetCamera(GetComponent
()); - }
-
- float width = 1080;//开发分辨率x
- float height = 1920;//开发分辨率y
-
- //需要适配宽度
- public void SetCamera(Camera camera)
- {
- var realWidthSize = (camera.orthographicSize * (width / height));
- Debug.Log($"width_{Screen.width}-height_{Screen.height}");
- float val = realWidthSize / ((float)Screen.width / (float)Screen.height);
- Debug.Log($"cameraSzie{val}");
- camera.orthographicSize = val;
- }
-
- }
完成