ThreeKingdomsSlingshot/Assets/Scripts/Runtime/UI/UiSafeArea.cs

156 lines
6.4 KiB
C#

using UnityEngine;
namespace TKS
{
/// <summary>
/// 안전 영역(노치 · 펀치홀 카메라 · 제스처 바 · PD 2026-11-26 해상도 대응 · Google Play 품질 「디스플레이 컷아웃 · edge-to-edge」) —
/// Android 15(targetSdk 35)부터 앱 화면이 컷아웃 밑까지 그려지는 것이 강제된다(Player 설정 Render Outside Safe Area 를 켠 것과 같음) →
/// 버튼 · 글자 · 재화 바 · 하단 탭은 Screen.safeArea 안에 두고, 바탕 그림 · 어둡게 깔기는 화면 끝까지 그대로 둔다.
/// </summary>
public static class UiSafeArea
{
/// <summary>캡처 · 시험용 가짜 여백(화면 비율 · x 왼 · y 아래 · z 오른 · w 위) — null = 실제 Screen.safeArea(에디터 Game 창 = 여백 0).</summary>
public static Vector4? Simulate;
/// <summary>화면 비율 여백(x 왼 · y 아래 · z 오른 · w 위 · 각 0~0.25).</summary>
public static Vector4 Fractions
{
get
{
if (Simulate.HasValue) return Simulate.Value;
float w = Screen.width, h = Screen.height;
if (w < 1f || h < 1f) return Vector4.zero;
var s = Screen.safeArea;
return new Vector4(Clamp(s.xMin / w), Clamp(s.yMin / h), Clamp((w - s.xMax) / w), Clamp((h - s.yMax) / h));
}
}
/// <summary>위 여백(화면 비율).</summary>
public static float Top
{
get { return Fractions.w; }
}
/// <summary>아래 여백(화면 비율).</summary>
public static float Bottom
{
get { return Fractions.y; }
}
static float Clamp(float v)
{
return Mathf.Clamp(v, 0f, 0.25f);
}
/// <summary>
/// 루트 캔버스의 지금 자식들을 안전 영역 칸(UiSafeFit) 안으로 옮긴다 — 화면을 꽉 채우는 자식(바탕 · 어둡게 깔기 = 앵커 0~1 · 여백 0)은 그대로 둔다.
/// 옮겨도 여백이 0 이면 모양 · 자리 변화 없음(칸 = 캔버스 전체). 나중에 캔버스 루트에 직접 붙는 자식은 옮기지 않으므로 만들기가 끝난 뒤 부른다.
/// </summary>
/// <param name="includeFull">true = 화면을 꽉 채우는 묶음 층(예 전투 HUD Info)도 옮김</param>
/// <param name="except">옮기지 않을 자식(예 결과 덮개)</param>
public static RectTransform Wrap(Transform canvasRoot, bool top = true, bool bottom = true, bool includeFull = false, Transform except = null)
{
if (canvasRoot == null) return null;
var kids = new System.Collections.Generic.List<Transform>();
foreach (Transform c in canvasRoot)
{
var rt = c as RectTransform;
if (rt == null || c == except || c.GetComponent<UiSafeFit>() != null) continue;
bool full = rt.anchorMin == Vector2.zero && rt.anchorMax == Vector2.one && rt.offsetMin == Vector2.zero && rt.offsetMax == Vector2.zero;
if (!full || includeFull) kids.Add(c);
}
var safe = new GameObject("SafeArea", typeof(RectTransform)).GetComponent<RectTransform>();
safe.SetParent(canvasRoot, false);
var fit = safe.gameObject.AddComponent<UiSafeFit>();
fit.top = top;
fit.bottom = bottom;
fit.Apply();
foreach (var c in kids)
{
int i = c.GetSiblingIndex();
c.SetParent(safe, false);
if (safe.GetSiblingIndex() > i) safe.SetSiblingIndex(i); // 그리는 순서 = 옮긴 첫 자식 자리
}
return safe;
}
}
/// <summary>부모(= 화면 전체 크기 루트)를 꽉 채우되 안전 영역 여백만큼 들인다(위 · 아래 · 옆 선택) — 크기 · 여백이 바뀔 때만 다시 계산.</summary>
[DisallowMultipleComponent]
public class UiSafeFit : MonoBehaviour
{
public bool top = true;
public bool bottom = true;
public bool sides = true;
Vector4 last = new Vector4(-1f, -1f, -1f, -1f);
void OnEnable()
{
last = new Vector4(-1f, -1f, -1f, -1f);
Apply();
}
void LateUpdate()
{
Apply();
}
public void Apply()
{
var rt = (RectTransform)transform;
var parent = rt.parent as RectTransform;
if (parent == null) return;
var f = UiSafeArea.Fractions;
var size = parent.rect.size;
var ins = new Vector4(sides ? f.x * size.x : 0f, bottom ? f.y * size.y : 0f, sides ? f.z * size.x : 0f, top ? f.w * size.y : 0f);
if (ins == last) return;
last = ins;
rt.anchorMin = Vector2.zero;
rt.anchorMax = Vector2.one;
rt.pivot = new Vector2(0.5f, 0.5f);
rt.offsetMin = new Vector2(ins.x, ins.y);
rt.offsetMax = new Vector2(-ins.z, -ins.w);
}
}
/// <summary>
/// 띠(재화 바 · 하단 탭) 바탕을 화면 끝까지 늘리는 덧댐 — top = 부모 윗변 위로 위 여백만큼 · false = 부모 아랫변 밑으로 아래 여백만큼(여백 0 = 높이 0).
/// 같은 바탕 그림 · 색을 깔아 노치 · 제스처 바 자리에 띠가 이어져 보이게 한다.
/// </summary>
[DisallowMultipleComponent]
public class UiSafeBleed : MonoBehaviour
{
public bool top = true;
float last = -1f;
void OnEnable()
{
last = -1f;
Apply();
}
void LateUpdate()
{
Apply();
}
void Apply()
{
var rt = (RectTransform)transform;
var canvas = GetComponentInParent<Canvas>();
if (canvas == null) return;
var croot = canvas.rootCanvas.transform as RectTransform;
if (croot == null) return;
var f = UiSafeArea.Fractions;
float h = (top ? f.w : f.y) * croot.rect.height;
if (Mathf.Abs(h - last) < 0.01f) return;
last = h;
rt.anchorMin = new Vector2(0f, top ? 1f : 0f);
rt.anchorMax = new Vector2(1f, top ? 1f : 0f);
rt.pivot = new Vector2(0.5f, top ? 0f : 1f);
rt.anchoredPosition = Vector2.zero;
rt.sizeDelta = new Vector2(0f, h);
}
}
}