SafeAreaAdapter.cs 984 B

12345678910111213141516171819202122232425
  1. using UnityEngine;
  2. namespace DevilLink2.Presentation
  3. {
  4. [RequireComponent(typeof(RectTransform))]
  5. public sealed class SafeAreaAdapter : MonoBehaviour
  6. {
  7. private Rect previous; private Vector2Int previousSize;
  8. private void OnEnable() { Apply(); }
  9. private void Update()
  10. {
  11. if (previous != Screen.safeArea || previousSize.x != Screen.width || previousSize.y != Screen.height) Apply();
  12. }
  13. public void Apply()
  14. {
  15. if (Screen.width <= 0 || Screen.height <= 0) return;
  16. var safe = Screen.safeArea; var rect = (RectTransform)transform;
  17. rect.anchorMin = new Vector2(safe.xMin / Screen.width, safe.yMin / Screen.height);
  18. rect.anchorMax = new Vector2(safe.xMax / Screen.width, safe.yMax / Screen.height);
  19. rect.offsetMin = rect.offsetMax = Vector2.zero;
  20. previous = safe; previousSize = new Vector2Int(Screen.width, Screen.height);
  21. }
  22. }
  23. }