반응형
문제
UI가 아니라 게임 오브젝트가 기기별로 해상도 대응이 안돼서 잘려서 표현이 된다.
이럴 때 해결 방법은 카메라 Rect 조절하기
방법
using UnityEngine;
public class CameraAdjust : MonoBehaviour
{
void Start()
{
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;
}
}
결과


반응형
'Unity > Tip' 카테고리의 다른 글
[Unity] warning CS0618: 'Object.FindObjectOfType(Type) 경고 (2) | 2024.10.02 |
---|---|
[Unity] SDK Build-tools component is not found 오류 (4) | 2024.09.26 |
[Unity] 컴파일 시간 단축시키기 (0) | 2024.06.17 |
[Unity] Aseprite: Animation Importer 에셋 (1) | 2023.12.02 |
[Unity] 유니티 씬뷰 오브젝트 클릭 안되는 오류(하이러키창에서만 선택되는 오류) (0) | 2023.11.26 |
반응형
문제
UI가 아니라 게임 오브젝트가 기기별로 해상도 대응이 안돼서 잘려서 표현이 된다.
이럴 때 해결 방법은 카메라 Rect 조절하기
방법
using UnityEngine;
public class CameraAdjust : MonoBehaviour
{
void Start()
{
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;
}
}
결과


반응형
'Unity > Tip' 카테고리의 다른 글
[Unity] warning CS0618: 'Object.FindObjectOfType(Type) 경고 (2) | 2024.10.02 |
---|---|
[Unity] SDK Build-tools component is not found 오류 (4) | 2024.09.26 |
[Unity] 컴파일 시간 단축시키기 (0) | 2024.06.17 |
[Unity] Aseprite: Animation Importer 에셋 (1) | 2023.12.02 |
[Unity] 유니티 씬뷰 오브젝트 클릭 안되는 오류(하이러키창에서만 선택되는 오류) (0) | 2023.11.26 |