|
- using System;
- using UnityEngine;
-
- namespace CIG
- {
- public class SafeAreaRect : MonoBehaviour
- {
- public bool Enabled { get; private set; }
-
- private void Start()
- {
- if (this._activateOnStart)
- {
- this.Enable();
- }
- }
-
- public void Enable()
- {
- if (!this.Enabled)
- {
- this.SetSafeAreaToUnitySafeArea();
- this.Enabled = true;
- }
- }
-
- public void Disable()
- {
- if (this.Enabled)
- {
- this.SetSafeArea(Vector2.zero, Vector2.one);
- this.Enabled = false;
- }
- }
-
- private void SetSafeAreaToUnitySafeArea()
- {
- Vector2 position = new Vector2(Screen.safeArea.position.x / (float)Screen.width, Screen.safeArea.position.y / (float)Screen.height);
- Vector2 size = new Vector2(Screen.safeArea.size.x / (float)Screen.width, Screen.safeArea.size.y / (float)Screen.height);
- this.SetSafeArea(position, size);
- }
-
- private void SetSafeArea(Vector2 position, Vector2 size)
- {
- this._rectTransform.anchorMin = position;
- this._rectTransform.anchorMax = position + size;
- }
-
- [SerializeField]
- private RectTransform _rectTransform;
-
- [SerializeField]
- private bool _activateOnStart = true;
- }
- }
|