Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

iOS: Screen.safeArea returns wrong results after Screen.SetResolution

Discussion in 'iOS and tvOS' started by dwit_mass_creation, Nov 15, 2018.

  1. dwit_mass_creation

    dwit_mass_creation

    Joined:
    Jun 18, 2015
    Posts:
    72
    I'm using Unity 2018.2.12.
    When I change resolution on iOS using Screen.SetResolution Screen.width and Screen.height is changing to new resolution. But Screen.safeArea still returns original resolution so it does not match new Screen.width and Screen.height.

    The problem is with method ComputeSafeArea in UnityView.mm which is getting safe area based on view size and not on current resolution.

    Will it be fixed in next version so changing resolution will also change safeArea?

    My current fixed method ComputeSafeArea:

    Code (CSharp):
    1. CGRect ComputeSafeArea(UIView* view)
    2. {
    3.     CGSize screenSize = view.bounds.size;
    4.     CGRect screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
    5.    
    6.     unsigned requestedW, requestedH;
    7.     UnityGetRenderingResolution(&requestedW, &requestedH);
    8.    
    9.     float scaleX = (float)requestedW / screenSize.width;
    10.     float scaleY = (float)requestedH / screenSize.height;
    11.  
    12.     UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
    13. #if UNITY_HAS_IOSSDK_11_0 || UNITY_HAS_TVOSSDK_11_0
    14.     if (@available(iOS 11.0, tvOS 11.0, *))
    15.         insets = [view safeAreaInsets];
    16. #endif
    17.  
    18.     screenRect.origin.x += insets.left;
    19.     screenRect.origin.y += insets.bottom; // Unity uses bottom left as the origin
    20.     screenRect.size.width -= insets.left + insets.right;
    21.     screenRect.size.height -= insets.top + insets.bottom;
    22.  
    23.     screenRect.origin.x *= scaleX;
    24.     screenRect.origin.y *= scaleY;
    25.     screenRect.size.width *= scaleX;
    26.     screenRect.size.height *= scaleY;
    27.  
    28.    
    29.     return screenRect;
    30. }
     
  2. dwit_mass_creation

    dwit_mass_creation

    Joined:
    Jun 18, 2015
    Posts:
    72
    It is still in Unity 2018.2.16. I posted bug (Case 1102732).
     
  3. Deleted User

    Deleted User

    Guest

    Screen.safeArea has changed on next frame.
    hope this tips can help you
     
  4. dwit_mass_creation

    dwit_mass_creation

    Joined:
    Jun 18, 2015
    Posts:
    72
    It was Unity bug and it was fixed in Unity 2018.3 so currently it should be no problem.