Unity EXE分辨率自适应

最近开发的项目的电脑分辨率和实际应用的不一样,为了避免现场分辨率出现同样的问题,就写了一个等比缩放的代码,主要思路是根据当前电脑屏幕的比值进行缩放。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetResolution : MonoBehaviour
{
    float systemwidth,systemheight;
    float finalwidth=3840, finalheight=2160;

    void Start()
    {
        Resolution[] resolutions = Screen.resolutions;

        systemwidth = resolutions[resolutions.Length - 1].width;
        systemheight = resolutions[resolutions.Length - 1].height;

        if (finalheight>systemheight)
        {
            
            finalwidth = (finalwidth / finalheight) * systemheight;
            finalheight = systemheight;
        }
        Screen.SetResolution((int)finalwidth, (int)finalheight, true);
    }
}

你可能感兴趣的:(unity,c#,游戏引擎)