SafeCanvas.cs 579 B

12345678910111213141516
  1. using UnityEngine;
  2. namespace DiamondArrow
  3. {
  4. public sealed class SafeCanvas : MonoBehaviour
  5. {
  6. public RectTransform content;
  7. void LateUpdate() { Apply(); }
  8. public void Apply()
  9. {
  10. if(content==null)return;float scale=1f/Mathf.Max(.001f,GetComponent<Canvas>().scaleFactor);Rect s=Screen.safeArea;
  11. content.anchoredPosition=new Vector2((s.center.x-Screen.width*.5f)*scale,(s.center.y-Screen.height*.5f)*scale);
  12. content.localScale=Vector3.one*Mathf.Min(s.width*scale/750,s.height*scale/1334);
  13. }
  14. }
  15. }