您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

56 行
1.2 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace CIG
  4. {
  5. public class SafeAreaRect : MonoBehaviour
  6. {
  7. public bool Enabled { get; private set; }
  8. private void Start()
  9. {
  10. if (this._activateOnStart)
  11. {
  12. this.Enable();
  13. }
  14. }
  15. public void Enable()
  16. {
  17. if (!this.Enabled)
  18. {
  19. this.SetSafeAreaToUnitySafeArea();
  20. this.Enabled = true;
  21. }
  22. }
  23. public void Disable()
  24. {
  25. if (this.Enabled)
  26. {
  27. this.SetSafeArea(Vector2.zero, Vector2.one);
  28. this.Enabled = false;
  29. }
  30. }
  31. private void SetSafeAreaToUnitySafeArea()
  32. {
  33. Vector2 position = new Vector2(Screen.safeArea.position.x / (float)Screen.width, Screen.safeArea.position.y / (float)Screen.height);
  34. Vector2 size = new Vector2(Screen.safeArea.size.x / (float)Screen.width, Screen.safeArea.size.y / (float)Screen.height);
  35. this.SetSafeArea(position, size);
  36. }
  37. private void SetSafeArea(Vector2 position, Vector2 size)
  38. {
  39. this._rectTransform.anchorMin = position;
  40. this._rectTransform.anchorMax = position + size;
  41. }
  42. [SerializeField]
  43. private RectTransform _rectTransform;
  44. [SerializeField]
  45. private bool _activateOnStart = true;
  46. }
  47. }