Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- css framework
- Spring Boot
- spread 연산자
- java
- screencapture
- nodejs
- Git
- draganddrop
- --watch
- mongoDB
- rpg server
- Packet Network
- docker
- MySQL
- linux
- Unity IAP
- critical rendering path
- Unity Editor
- Google Developer API
- express
- react
- unity
- Digital Ocean
- OverTheWire
- SDK upgrade
- server
- springboot
- Camera Zoom
- Google Refund
- Camera Movement
Archives
- Today
- Total
우당탕탕 개발일지
[Unity] 모바일 디바이스 노치 영역 처리하기 본문
핸드폰 전면에 있는 상단 카메라 , 그리고 아이폰의 경우 하단 막대기 부분 영역에 게임요소가 들어가지 않게 처리하는 방법에 대해 알아보자
그리고, 노치영역이 이쁘게 나왔으면 좋겠으니까 데코이미지로 꾸미는 방법도 알아보자.
참고로 , 이 노치 영역 처리는 메인 캔버스가 ScreenSpace - Camera일때 사용한다.
. 왜냐하면 이 스크립트는 Camera의 영역을 조절하는 방법을 사용하기 때문이다
노치영역 제외하기
사용하는 메인 카메라에 다음 스크립트를 붙여넣는다.
카메라가 노치영역을 아예 보여주지 않도록 설정하는 함수이다.
using UnityEngine;
[ExecuteAlways]
public class SafeAreaCameraCrop : MonoBehaviour
{
void Start()
{
ApplySafeAreaToCamera();
}
void ApplySafeAreaToCamera()
{
var safe = Screen.safeArea;
float x = safe.x / Screen.width;
float y = safe.y / Screen.height;
float w = safe.width / Screen.width;
float h = safe.height / Screen.height;
Camera.main.rect = new Rect(x, y, w, h);
}
}
자동으로 노치영역을 제외하고 보여준다.
노치영역 꾸미기
우선 씬에 새로운 Canvas 를 추가하고, 모드는 ScreenSpace - Overlay 로 설정한다.
캔버스에 아래 스크립트를 부착한다. 아이폰의 경우 아래 노치도 있으므로, TopUI 와 BottomUI 두개를 제작한다. 카메라보다 UI가 더 위기이때문에 , Overlay 는 카메라영역을 무시하고 이미지를 뿌린다. 이 특징을 이용해 노치영역을 꾸민다.
using UnityEngine;
using UnityEngine.UI;
[ExecuteAlways]
public class NotchDecorator : MonoBehaviour
{
[SerializeField] private RectTransform topUI;
[SerializeField] private RectTransform bottomUI;
private void Start()
{
var scaler = gameObject.GetComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(Globals.SCREEN_WIDTH, Globals.SCREEN_HEIGHT);
scaler.matchWidthOrHeight = 0;
Apply();
}
private void Apply()
{
Rect safeArea = Screen.safeArea;
float screenHeight = Screen.height;
// 상단 노치 영역 높이
float topNotchHeight = screenHeight - (safeArea.y + safeArea.height);
// 하단 노치 영역 높이
float bottomNotchHeight = safeArea.y;
if (topUI != null)
{
topUI.anchorMin = new Vector2(0, 1);
topUI.anchorMax = new Vector2(1, 1);
topUI.pivot = new Vector2(0.5f, 1);
topUI.offsetMin = new Vector2(0, -topNotchHeight);
topUI.offsetMax = Vector2.zero;
}
if (bottomUI != null)
{
bottomUI.anchorMin = new Vector2(0, 0);
bottomUI.anchorMax = new Vector2(1, 0);
bottomUI.pivot = new Vector2(0.5f, 0);
bottomUI.offsetMin = Vector2.zero;
bottomUI.offsetMax = new Vector2(0, bottomNotchHeight);
}
}
}
이쁘게 꾸며보자고~
'Unity' 카테고리의 다른 글
| [Unity] GPGS 2.0.0 로그인 에러 해결 (1) | 2025.06.13 |
|---|---|
| [Unity] 커스텀에디터 - ReorderableList 사용하기 (0) | 2025.05.07 |
| [Unity IOS] 빌드에러 Building Library/Bee/artifacts/iOS/ManagedStripped failed (0) | 2025.04.24 |
| [커스텀 에디터] 파일 이름에서 ID추출 & 자동 ID부여하기 (0) | 2025.04.08 |
| [Unity] CustomEditor - Dictionary 사용하기 (0) | 2025.03.17 |