레터박스: 화면의 가로세로 비율(aspect ratio)이 게임이 설정된 비율과 다를 때 빈 공간을 화면의 상단/하단 또는 좌우에 채워주는 방식
초동수사는 화면의 가로세로 비율을 고정시켜 놓는다.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class CameraResolution : MonoBehaviour
{
private void Awake()
{
// 현재 GameObject에 부착된 Camera 컴포넌트를 가져오는 코드
Camera cam = GetComponent<Camera>();
// 현재 카메라의 뷰포트 영역을 가져오는 코드
Rect viewportRect = cam.rect;
// 원하는 가로 세로 비율을 계산하는 코드
float screenAspectRatio = (float)Screen.width / Screen.height;
// float targetAspectRatio = 22f / 9f; // 테스트
float targetAspectRatio = 13f / 6f; // 원하는 고정 비율 설정 (예: 16:9)
// 화면 가로 세로 비율에 따라 뷰포트 영역을 조정하는 코드
if (screenAspectRatio < targetAspectRatio)
{
// 화면이 더 '높다'면 (세로가 더 길다면) 세로를 조절하는 코드
viewportRect.height = screenAspectRatio / targetAspectRatio;
viewportRect.y = (1f - viewportRect.height) / 2f;
}
else
{
// 화면이 더 '넓다'면 (가로가 더 길다면) 가로를 조절하는 코드.
viewportRect.width = targetAspectRatio / screenAspectRatio;
viewportRect.x = (1f - viewportRect.width) / 2f;
}
// 조정된 뷰포트 영역을 카메라에 설정하는 코드
cam.rect = viewportRect;
}
}
다른 게임에도 항상 이걸 쓰는데... 아이폰의 경우는 거의 대부분 레터박스가 안 보이거나 조금 보인다. 애플만세.
하지만 아이패드와 갤럭시는 어쩔 수 없이 경우가 다른데, 레터박스가 잘 보이는 경우 아래와 같은 현상이 나온다.