Unity 해상도 설정

삐얅·2024년 7월 8일
0

유니티

목록 보기
7/20

1. 개요

팀 프로젝트 진행 중 해상도 설정이 필요해서 작업했다. 리스트를 사용해 해상도 값들을 담아주고 구조체를 이용해 텍스트에 반영되게 했다.

2. 코드

using BehaviorDesigner.Runtime.Tasks;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEditor;
using System.Reflection;

public class VideoSetting : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI currentResolution;

	// 초기 해상도들을 담으려고 만들었다.
    private int screenWidth;
    private int screenHeight;
    //해상도는 좌 우 버튼을 통해 변경되니 이를 참고할 index
    private int index = 0;

    private void Start()
    {
    	// 현재 해상도를 반영한다.
        CurrentResolution();
    }

	// 구조체
    private struct ResolutionOption
    {
        public int width;
        public int height;

        public ResolutionOption(int w, int h)
        {
            width = w;
            height = h;
        }

		// 넓이와 높이를 반환한다
        public override string ToString()
        {
            return $"{width} x {height}";
        }
    }

	// 해상도들을 담을 리스트, 구조체들을 만들어 담아준다
    private List<ResolutionOption> resolutions = new List<ResolutionOption>
    {
        new ResolutionOption(1920, 1080),
        new ResolutionOption(1280, 720),
        new ResolutionOption(800, 600)
    };

	// 현재 해상도
    private void CurrentResolution()
    {
        screenWidth = Screen.width;
        screenHeight = Screen.height;

        currentResolution.text = $"{screenWidth} x {screenHeight}";
    }

	// 다음 해상도로 넘긴다. 길이보다 커질 경우 0으로 만든다.
    // 아래 주석 코드는 에디터 상에서 확인하려고 만들었는데 에디터의 해상도 index를 반영하기 복잡해져서 일단 만들어만뒀다.
    public void NextResolution()
    {
        index++;
        if(index >= resolutions.Count)
        {
            index = 0;
        }
        UpdateResolutionText();
        SetResolution();
        /*
        var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
        var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        var gvWnd = EditorWindow.GetWindow(gvWndType);
        selectedSizeIndexProp.SetValue(gvWnd, index, null);*/
    }

	// 이전 해상도, 0보다 작아지면 리스트의 마지막 구조체로 이동한다.
    public void PreviousResolution()
    {
        index--;
        if(index < 0)
        {
            index = resolutions.Count - 1;
        }
        UpdateResolutionText();
        SetResolution();
    }

	// 텍스트를 업데이트 한다. 리스트가 가지고 있는 구조체의 ToString()을 반영
    private void UpdateResolutionText()
    {
        currentResolution.text = resolutions[index].ToString();
    }

	// 바뀐 해상도를 반영해준다.
    private void SetResolution()
    {
        ResolutionOption resolutionOption = resolutions[index];
        Screen.SetResolution(resolutionOption.width, resolutionOption.height, Screen.fullScreen);
    }
}

3. 정리

해상도 설정을 만들었으나, 에디터 상에서는 수정된 사항을 바로 볼 수 없어서 당황스러웠다. 튜터님께 질문해본 결과 위 주석 처리된 코드로 에디터의 해상도를 바꿔줄 수 있는데 현재 해상도 설정을 바꾸는 부분과 index를 매치시키기 어려워 주석으로만 남겨뒀다. 필요한일 있으면 가져다 쓸 수 있으니....
다음엔 추가로 창모드까지 생각은 해뒀으나 일단 문제가 해상도가 바뀌면 ui 위치도 바뀐다는 것...
해상도를 바꿔가며 ui 위치들을 봐줘야 하는 추가적인 작업이 생겼다...
다음에 해상도 설정을 할 일이 있다면 우선순위로 두고 작업해야겠다.

0개의 댓글

관련 채용 정보