2017年7月16日 星期日

更改螢幕大小(解析度)

因由之前製作遊戲過程當中,想要讓玩家調整螢幕之解析度,因此測試了這項的小功能。
意外發現到與Unity本身發佈出來後之解析度有些不同 (目前小弟還在進行研究當中)。

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

public class ScreenResolution : MonoBehaviour
{
    // resolutoion X, Y
    public List resolutionX = new List();
    public List resolutionY = new List();

    public int status;

    void Start()
    {
        resolutionFiltering();
    }

    void OnGUI()
    {
        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height, 200, Screen.height));

        if (GUILayout.Button("螢幕解析度切換+"))
            status += 1;

        if (status >= resolutionX.Count && status >= resolutionY.Count)
            status = 0;

        if (GUILayout.Button("確定"))
            Screen.SetResolution(resolutionX[status], resolutionY[status], true);
    

        GUILayout.Label(resolutionX[status].ToString() + " + " + resolutionY[status].ToString());
        GUILayout.Label("目前解析度:" + Screen.width + " + " + Screen.height);
        GUILayout.EndArea();

        Screen.fullScreen = false;  // 不全屏: false  全屏: true
    }

    /// 
    /// 螢幕解析度
    /// 過濾重複的數值
    /// 
    void resolutionFiltering()
    {
        Resolution[] resolutions = Screen.resolutions;

        foreach (Resolution res in resolutions)
        {
            resolutionX.Add(res.width);
            resolutionY.Add(res.height);
        }

        for (int i = resolutionX.Count - 1; i > 0; i--)
        {
            if (resolutionX.IndexOf(resolutionX[i]) != i)
                resolutionX.RemoveAt(i);
        }

        for (int i = resolutionY.Count - 1; i > 0; i--)
        {
            if (resolutionY.IndexOf(resolutionY[i]) != i)
                resolutionY.RemoveAt(i);
        }
    }
}

------------
結果圖:




沒有留言:

張貼留言