unity 设置分辨率

按照比例设置目标分辨率:

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

public class CurrentScreenResolution : MonoBehaviour
{
    public Text intfoText;
    [Header("分辨率 Width")]
    public float screenWidth = 4096f;
    [Header("分辨率 Height")]
    public float screenHeight = 768f;

    void Awake()
    {
        Debug.Log(transform.name);
        MultScreen();
    }
    void Start()
    {
        Vector2 resolution = VivewResolution();
        Screen.SetResolution((int)resolution.x, (int)resolution.y, true);
    }

    private void Update()
    {

    }

    private void MultScreen()
    {
        intfoText.text = "检测到显示器数量为:" + Display.displays.Length;
        for (int i = 0; i < Display.displays.Length; i++)
        {
            Display.displays[i].Activate();
            Screen.SetResolution(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, true);
        }
    }


    private Vector2 VivewResolution()
    {
        int currentWidth = Screen.width;
        int width = currentWidth;
        float height = currentWidth / screenWidth * screenHeight;
        Debug.Log(currentWidth / screenWidth);

        Debug.Log("实际高度:" + height);
        return new Vector2(width, height);
    }
}

强制按照指定比例显示,效果如下:

SetScreenResolution

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