ThreeKingdomsSlingshot/Assets/Scripts/OutGame/UI/UiTallLayout.cs

89 lines
3.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
namespace TKS.OutGame
{
/// <summary>
/// 세로로 긴 화면(20:9 등)에서 목업 본문(1080 × BodyH · 위에 붙음)이 남기는 아래 빈 높이를 쓰는 법(PD 2026-11-26 해상도 대응 「영웅 상세 · 편성 아래가 크게 빔」).
/// 남는 높이 extra = 부모(본문 Fit) 높이 − designH(0 이하 = 기준 화면 · 변화 없음).
/// ① splitY ≥ 0 = 목업 위에서 splitY px 아래에서 시작하는 자식(위에 붙은 것)을 extra 만큼 아래로(가운데가 벌어짐 · 바탕 그림이 더 보임)
/// ② stretch = 아래끝을 extra 만큼 늘릴 것(목록 판 · 스크롤 목록) — 위에 붙은 것은 높이 + extra · 세로로 늘어나는 것은 아래 여백 − extra.
/// 자식 목록 · 원래 자리는 첫 계산 때 기억(그 뒤 새로 붙는 자식은 건드리지 않음).
/// </summary>
public class UiTallLayout : MonoBehaviour
{
public float designH;
public float splitY = -1f;
public readonly List<RectTransform> stretch = new List<RectTransform>();
struct Orig
{
public RectTransform rt;
public Vector2 pos;
public Vector2 size;
public Vector2 offMin;
public bool shift;
public bool grow;
}
readonly List<Orig> orig = new List<Orig>();
float last = -1f;
/// <summary>지금 남는 높이(시험 · 캡처 로그).</summary>
public float Extra { get; private set; }
void OnEnable()
{
Apply();
}
void LateUpdate()
{
Apply();
}
void Remember()
{
orig.Clear();
var me = (RectTransform)transform;
foreach (Transform t in me)
{
var rt = t as RectTransform;
if (rt == null) continue;
bool grow = stretch.Contains(rt);
bool topAnch = Mathf.Approximately(rt.anchorMin.y, 1f) && Mathf.Approximately(rt.anchorMax.y, 1f);
bool botAnch = Mathf.Approximately(rt.anchorMin.y, 0f) && Mathf.Approximately(rt.anchorMax.y, 0f);
bool shift = !grow && botAnch; // 목업 아래끝에 붙은 것(목록 아래 페이드 등) = 늘어난 아래끝을 따라감
if (!grow && splitY >= 0f && topAnch)
{
float top = -(rt.anchoredPosition.y + (1f - rt.pivot.y) * rt.sizeDelta.y); // 목업 위에서 이 자식 윗변까지(px)
shift = top >= splitY - 0.5f;
}
if (grow || shift) orig.Add(new Orig { rt = rt, pos = rt.anchoredPosition, size = rt.sizeDelta, offMin = rt.offsetMin, shift = shift, grow = grow });
}
}
public void Apply()
{
var parent = transform.parent as RectTransform;
if (parent == null || designH <= 0f) return;
float extra = Mathf.Max(0f, Mathf.Round(parent.rect.height - designH));
if (Mathf.Abs(extra - last) < 0.5f) return;
if (last < 0f) Remember();
last = extra;
Extra = extra;
foreach (var o in orig)
{
if (o.rt == null) continue;
if (o.shift) o.rt.anchoredPosition = new Vector2(o.pos.x, o.pos.y - extra);
else if (o.grow)
{
bool topAnch = Mathf.Approximately(o.rt.anchorMin.y, 1f) && Mathf.Approximately(o.rt.anchorMax.y, 1f);
if (topAnch) o.rt.sizeDelta = new Vector2(o.size.x, o.size.y + extra);
else o.rt.offsetMin = new Vector2(o.offMin.x, o.offMin.y - extra);
}
}
}
}
}