Unity 主摄像机正交模式下适配
下图是 Canvas 上UI 自动适配宽度
如果要非UGUI的对象在正交模式下也类似UI能适配宽度,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 正交相机模式下UGUI 适配不同分辨率
/// </summary>
public class CameraAdaptive : MonoBehaviour
{
public float fDesignScreenWidth;
public float fDesignScreenHeight;
public Camera mainCamera;
private void Start()
{
var designRatio = fDesignScreenWidth / fDesignScreenHeight;
var realRatio = Screen.width / (float)Screen.height;
var main = Camera.main;
if (main is null) return;
var tarSize = designRatio * main.orthographicSize / realRatio;
main.orthographicSize = tarSize;
mainCamera.orthographicSize = tarSize;
}
}