- UI/SafeArea: подгоняет контейнер под Screen.safeArea, следит за поворотом - UI/SafeAreaInstaller: при загрузке сцены переносит элементы HUD_Canvas и WorldMap_Canvas в контейнер SafeArea; окна на весь экран остаются на весь экран Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
1.5 KiB
C#
39 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
// Подгоняет RectTransform под безопасную зону экрана (Screen.safeArea): вырез камеры, скруглённые углы,
|
|
// полоска "домой" на iPhone. Следит за поворотом и сменой разрешения (и в Device Simulator).
|
|
// Вешать на растянутый на весь Canvas объект; всё, что внутри, не залезет под вырез.
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class SafeArea : MonoBehaviour
|
|
{
|
|
private RectTransform rect;
|
|
private Rect appliedArea;
|
|
private Vector2Int appliedScreen;
|
|
|
|
void Awake()
|
|
{
|
|
rect = GetComponent<RectTransform>();
|
|
Apply();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Screen.safeArea != appliedArea || Screen.width != appliedScreen.x || Screen.height != appliedScreen.y)
|
|
Apply();
|
|
}
|
|
|
|
void Apply()
|
|
{
|
|
Rect area = Screen.safeArea;
|
|
appliedArea = area;
|
|
appliedScreen = new Vector2Int(Screen.width, Screen.height);
|
|
if (Screen.width <= 0 || Screen.height <= 0) return;
|
|
|
|
// Якоря в долях экрана: Canvas в режиме Screen Space Overlay покрывает экран целиком
|
|
rect.anchorMin = new Vector2(area.xMin / Screen.width, area.yMin / Screen.height);
|
|
rect.anchorMax = new Vector2(area.xMax / Screen.width, area.yMax / Screen.height);
|
|
rect.offsetMin = Vector2.zero;
|
|
rect.offsetMax = Vector2.zero;
|
|
}
|
|
}
|