1. 程式人生 > >Unity寬度適配的程式碼

Unity寬度適配的程式碼

將該程式碼附加到camera上,devHeight和devWidth修改為設計解析度

using UnityEngine;
public class GameCamera : MonoBehaviour
{
    float devHeight = 1920f;
    float devWidth = 1080f;

    void Start()
    {
        float orthographicSize = this.GetComponent<Camera>().orthographicSize;
        orthographicSize = orthographicSize / (devHeight / devWidth) * (Screen.height * 1.0f / Screen.width);
        this.GetComponent<Camera>().orthographicSize = orthographicSize;
    }
}

攝像機尺寸Size除以設計時的高寬比,再乘以實際螢幕高寬比,得到同寬度時應設定的Size