[유니티 Unity] Unity URP에서 Letterbox 사용하기

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Rendering;

​

public class CameraResolution : MonoBehaviour

{

public void onSetting()

{

Camera camera = GetComponent<Camera>();

Rect rect = camera.rect;

float scaleheight = ((float)Screen.width / Screen.height) / ((float)9 / 16); // (가로 / 세로)

float scalewidth = 1f / scaleheight;

if (scaleheight < 1)

{

rect.height = scaleheight;

rect.y = (1f - scaleheight) / 2f;

}

else

{

rect.width = scalewidth;

rect.x = (1f - scalewidth) / 2f;

}

camera.rect = rect;

}

​

public void OnReset()

{

Camera camera = GetComponent<Camera>();

camera.rect = new Rect(0, 0, 1, 1);

}

​

void OnEnable()

{

#if !UNITY_EDITOR

RenderPipelineManager.beginCameraRendering += RenderPipelineManager_endCameraRendering;

#endif

}

void OnDisable()

{

#if !UNITY_EDITOR

RenderPipelineManager.beginCameraRendering -= RenderPipelineManager_endCameraRendering;

#endif

}

​

​

private void RenderPipelineManager_endCameraRendering(ScriptableRenderContext context, Camera camera)

{

GL.Clear(true, true, Color.black);

}

​

void OnPreCull()

{

GL.Clear(true, true, Color.black);

}

}

출처 : ​https://m.blog.naver.com/kj426/222131335427
public class ScreenFixed : MonoBehaviour
{

    public Camera camera;


    private void Start()
    {
        SetResolution(); // 초기에 게임 해상도 고정
        //Camera2View();
    }

    /* 해상도 설정하는 함수 */
    public void SetResolution()
    {
        int setWidth = 2560; // 사용자 설정 너비
        int setHeight = 1440; // 사용자 설정 높이

        int deviceWidth = Screen.width; // 기기 너비 저장
        int deviceHeight = Screen.height; // 기기 높이 저장

        Screen.SetResolution(setWidth, (int)(((float)deviceHeight / deviceWidth) * setWidth), true); // SetResolution 함수 제대로 사용하기

        if ((float)setWidth / setHeight < (float)deviceWidth / deviceHeight) // 기기의 해상도 비가 더 큰 경우
        {
            float newWidth = ((float)setWidth / setHeight) / ((float)deviceWidth / deviceHeight); // 새로운 너비
            camera.rect = new Rect((1f - newWidth) / 2f, 0f, newWidth, 1f); // 새로운 Rect 적용
        }
        else // 게임의 해상도 비가 더 큰 경우
        {
            float newHeight = ((float)deviceWidth / deviceHeight) / ((float)setWidth / setHeight); // 새로운 높이
            camera.rect = new Rect(0f, (1f - newHeight) / 2f, 1f, newHeight); // 새로운 Rect 적용
        }
    }

    public void Camera2View()
    {
        Rect rt = camera.rect;

        float scale_height = ((float)Screen.width / Screen.height) / ((float)16 / 9);   //가로
        float scale_width = 1f / scale_height;

        if(scale_height < 1)
        {
            rt.height = scale_height;
            rt.y = (1f - scale_height) / 2f;
        }
        else
        {
            rt.width = scale_width;
            rt.x = (1f - scale_width) / 2f;
        }

        camera.rect = rt;
    }

}