Record/TIL

[Unity] 유니티 해상도 드롭박스로 조절하기

석영 2024. 8. 5. 21:56
반응형

Resolution

화면 해상도를 나타내는 구조체
  • width: 해상도의 너비를 픽셀 단위로 나타낸다.
  • height: 해상도의 높이를 픽셀 단위로 나타낸다.
  • refreshRate: 해상도의 수직 재생률을 Hz 단위로 나타낸다

 

코드

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

public class GraphicManager : MonoBehaviour
{
    [SerializeField] private TMP_Dropdown resolutionsDropdown;
    List<Resolution> resolutions = new List<Resolution>();
    int resolutionNum;

    private void Start()
    {
        InitUI();
    }

    private void InitUI()
    {
        resolutions.AddRange(Screen.resolutions);
        resolutionsDropdown.options.Clear();

        int optionNum = 0;

        foreach (Resolution item in resolutions)
        {
            TMP_Dropdown.OptionData option = new TMP_Dropdown.OptionData();
            option.text = item.width + "x" + item.height + " " + item.refreshRate + "hz";
            resolutionsDropdown.options.Add(option);

            if (item.width == Screen.width && item.height == Screen.height)
            {
                resolutionsDropdown.value = optionNum;
            }
            optionNum++;
        }

        resolutionsDropdown.RefreshShownValue();
    }

    public void OnResolutionChange(int x)
    {
        resolutionNum = x;
        Resolution selectedResolution = resolutions[resolutionNum];
        SetResolution(selectedResolution.width, selectedResolution.height);
    }

    private void SetResolution(int width, int height)
    {
        PlayerPrefs.SetInt("ScreenWidth", width);
        PlayerPrefs.SetInt("ScreenHeight", height);
        PlayerPrefs.Save();

        Screen.SetResolution(width, height, Screen.fullScreenMode);
    }

 

 

연결

 

 

 

반응형