SafeArea.cs 762 B

12345678910111213141516171819202122
  1. using UnityEngine;
  2. namespace Olympus
  3. {
  4. [RequireComponent(typeof(RectTransform))]
  5. public sealed class SafeArea : MonoBehaviour
  6. {
  7. Rect last;
  8. int width, height;
  9. void LateUpdate()
  10. {
  11. var area = Screen.safeArea;
  12. if (area == last && width == Screen.width && height == Screen.height) return;
  13. last = area; width = Screen.width; height = Screen.height;
  14. if (width < 1 || height < 1) return;
  15. var rect = (RectTransform)transform;
  16. rect.anchorMin = new Vector2(area.xMin / width, area.yMin / height);
  17. rect.anchorMax = new Vector2(area.xMax / width, area.yMax / height);
  18. rect.offsetMin = rect.offsetMax = Vector2.zero;
  19. }
  20. }
  21. }