Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

no Work SafeArea

Discussion in 'Android' started by darknside, Oct 2, 2021.

  1. darknside

    darknside

    Joined:
    May 4, 2021
    Posts:
    14
    Hello!

    Can you please tell me why the lower part of the menu can be blocked?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Events;
    6.  
    7. [RequireComponent(typeof(Canvas))]
    8. public class CanvasHelper : MonoBehaviour
    9. {
    10.     // Thank you to Adriaan de Jongh https://twitter.com/adriaandejongh
    11.     // author of Hidden Folks
    12.     // for the bulk of the script below
    13.     public static UnityEvent onOrientationChange = new UnityEvent();
    14.     public static UnityEvent onResolutionChange = new UnityEvent();
    15.     public static bool isLandscape { get; private set; }
    16.  
    17.     private static List<CanvasHelper> helpers = new List<CanvasHelper>();
    18.  
    19.     private static bool screenChangeVarsInitialized = false;
    20.     private static ScreenOrientation lastOrientation = ScreenOrientation.Portrait;
    21.     private static Vector2 lastResolution = Vector2.zero;
    22.     private static Rect lastSafeArea = Rect.zero;
    23.  
    24.     private Canvas canvas;
    25.     private RectTransform rectTransform;
    26.     private RectTransform safeAreaTransform;
    27.    
    28.     void Awake()
    29.     {
    30.         if (!helpers.Contains(this))
    31.             helpers.Add(this);
    32.         canvas = GetComponent<Canvas>();
    33.         rectTransform = GetComponent<RectTransform>();
    34.         // the canvas/panel you want to react to the SafeArea needs to be called "SafeArea",
    35.         // and be a child of the top canvas where this script is attached
    36.         safeAreaTransform = transform.Find("SafeArea") as RectTransform;
    37.         if (!screenChangeVarsInitialized)
    38.         {
    39.             lastOrientation = Screen.orientation;
    40.             lastResolution.x = Screen.width;
    41.             lastResolution.y = Screen.height;
    42.             lastSafeArea = Screen.safeArea;
    43.             screenChangeVarsInitialized = true;
    44.         }
    45.     }
    46.  
    47.     void Start()
    48.     {
    49.         ApplySafeArea();
    50.     }
    51.  
    52.     void Update()
    53.     {
    54.         if (helpers[0] != this)
    55.             return;
    56.         if (Screen.orientation != lastOrientation)
    57.             OrientationChanged();
    58.         if (Screen.safeArea != lastSafeArea)
    59.             SafeAreaChanged();
    60.         if (Screen.width != lastResolution.x || Screen.height != lastResolution.y)
    61.                 ResolutionChanged();    
    62.     }
    63.  
    64.     void ApplySafeArea()
    65.     {      
    66.         if (safeAreaTransform == null)
    67.             return;
    68.  
    69.         var safeArea = Screen.safeArea;
    70.         var anchorMin = safeArea.position;
    71.         var anchorMax = safeArea.position + safeArea.size;
    72.         anchorMin.x /= canvas.pixelRect.width;
    73.         anchorMin.y /= canvas.pixelRect.height;
    74.         anchorMax.x /= canvas.pixelRect.width;
    75.         anchorMax.y /= canvas.pixelRect.height;
    76.         safeAreaTransform.anchorMin = anchorMin;
    77.         safeAreaTransform.anchorMax = anchorMax;
    78.  
    79.         Debug.Log(
    80.         "Applied SafeArea:" +
    81.          "\n Screen.orientation: " + Screen.orientation +
    82.          "\n Screen.safeArea.position: " + Screen.safeArea.position.ToString() +
    83.          "\n Screen.safeArea.size: " + Screen.safeArea.size.ToString() +
    84.          "\n Screen.width / height: (" + Screen.width.ToString() + ", " + Screen.height.ToString() + ")" +
    85.          "\n canvas.pixelRect.size: " + canvas.pixelRect.size.ToString() +
    86.          "\n anchorMin: " + anchorMin.ToString() +
    87.          "\n anchorMax: " + anchorMax.ToString());
    88.     }
    89.  
    90.     void OnDestroy()
    91.     {
    92.         if (helpers != null && helpers.Contains(this))
    93.             helpers.Remove(this);
    94.     }
    95.  
    96.     private static void OrientationChanged()
    97.     {
    98.         Debug.Log("Orientation changed from " + lastOrientation + " to " + Screen.orientation + " at " + Time.time);
    99.  
    100.         lastOrientation = Screen.orientation;
    101.         lastResolution.x = Screen.width;
    102.         lastResolution.y = Screen.height;
    103.  
    104.         isLandscape = lastOrientation == ScreenOrientation.LandscapeLeft || lastOrientation == ScreenOrientation.LandscapeRight || lastOrientation == ScreenOrientation.Landscape;
    105.         onOrientationChange.Invoke();
    106.  
    107.     }
    108.  
    109.     private static void ResolutionChanged()
    110.     {
    111.         if (lastResolution.x == Screen.width && lastResolution.y == Screen.height)
    112.             return;
    113.  
    114.         Debug.Log("Resolution changed from " + lastResolution + " to (" + Screen.width + ", " + Screen.height + ") at " + Time.time);
    115.  
    116.         lastResolution.x = Screen.width;
    117.         lastResolution.y = Screen.height;
    118.  
    119.         isLandscape = Screen.width > Screen.height;
    120.         onResolutionChange.Invoke();
    121.     }
    122.  
    123.     private static void SafeAreaChanged()
    124.     {
    125.         if (lastSafeArea == Screen.safeArea)
    126.             return;
    127.  
    128.         Debug.Log("Safe Area changed from " + lastSafeArea + " to " + Screen.safeArea.size + " at " + Time.time);
    129.  
    130.         lastSafeArea = Screen.safeArea;
    131.  
    132.         for (int i = 0; i < helpers.Count; i++)
    133.         {
    134.             helpers[i].ApplySafeArea();
    135.         }
    136.     }
    137.  
    138.     public static Vector2 GetCanvasSize()
    139.     {
    140.         return helpers[0].rectTransform.sizeDelta;
    141.     }
    142.  
    143.     public static Vector2 GetSafeAreaSize()
    144.     {
    145.         for (int i = 0; i < helpers.Count; i++)
    146.         {
    147.             if (helpers[i].safeAreaTransform != null)
    148.             {
    149.                 return helpers[i].safeAreaTransform.sizeDelta;
    150.             }
    151.         }
    152.  
    153.         return GetCanvasSize();
    154.     }
    155. }
     
  2. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    I'm not sure if I understood your problem correctly. But I think the problem is that the UICanvas is using UI space coordinates ( x:0, y:0 is at top left corner), and the safeArea is using ScreenSpace coordinates ( x:0, y:0, is at bottom left corner). So "var anchorMin = safeArea.position; var anchorMax = safeArea.position + safeArea.size; " results in upside down position.