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. Dismiss Notice

Screen.safeArea not correct for Pixel 4, Unity 2018.4

Discussion in 'UGUI & TextMesh Pro' started by alan-lawrance, Aug 25, 2020.

  1. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    Unity 2018.4.23, Screen.safeArea is returning a zero offset, but the phone has rounded corners. Tested this in an update loop, so it's not an Awake/Start issue.

    Are there known problems with Screen.safeArea and Android in 2018.4?

    Note: Pixel 4 runs Android 10.
     
    Last edited: Aug 25, 2020
  2. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    Tested with latest 2018.4 available (2018.3.26f1) and same results. Safe area is the full screen.
     
  3. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    In case API level is a factor:

    Minimum API level is 19, Target API level is highest available (which is shown as level 28).

    Are other people using Screen.safearea OK on Android? Maybe it's just best to assume a worst case, and move all UI away from the edges.
     
    Last edited: Aug 26, 2020
  4. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    Bump... anyone out there using Screen.safearea successfully on Android for 2018.8.4?
     
  5. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    Is this the wrong forum to ask this question?
     
  6. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    Someone will find this thread in 2025 and wish someone had followed up.
     
  7. KonstantinMaximus

    KonstantinMaximus

    Joined:
    Feb 13, 2014
    Posts:
    21
    It's not 2025, but I do wish someone responded to this. Have the same issue. Did you ever find a solution? I am on 2019.4.4
     
  8. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    353
    I was never able to find a way to get a non-zero safe area. The work-around was to just assume a worst-case safe area, which really sucks.
     
  9. KonstantinMaximus

    KonstantinMaximus

    Joined:
    Feb 13, 2014
    Posts:
    21
    That's unfortunate. I tried 2020x, 2019x, SDK 28. Doesnt work on Android, fine on iOS :(
     
  10. ft_cjy

    ft_cjy

    Joined:
    Sep 28, 2020
    Posts:
    2
    2019.4.23LTS in android. Same Problem. Safe area is the full screen, and Screen.Cutouts is empty.
     
  11. RLin

    RLin

    Joined:
    May 10, 2015
    Posts:
    133
    Experiencing the same issue - anyone have a fix for this? Without a way to detect rounded corners, UI elements in my game end up getting cut off.
     
  12. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    Is it a Unity issue or is the phone not reporting any safe areas? If it's the latter, there isn't much Unity can do.
     
  13. RLin

    RLin

    Joined:
    May 10, 2015
    Posts:
    133
    Using the device simulator, the iPhone X has a correct safe area like this:
    upload_2021-5-15_17-17-19.png
    On the other hand, the Pixel 4's safe area goes outside of the corners:
    upload_2021-5-15_17-17-57.png
    This is strange because the device simulator clearly recognizes that the Pixel 4 has rounded corners, yet the safe area ignores them.
     
  14. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    The safe areas are something that the phone itself reports, there are iOS APIs and Android APIs that Unity then takes and maps to Screen.safeArea.

    If the phone itself doesn't report any insets / safe areas, then there's not much for Unity to do.
     
  15. RLin

    RLin

    Joined:
    May 10, 2015
    Posts:
    133
    If this is the case, I'm wondering how other developers handle this? Especially if phones as prominent as the Google Pixel don't report this.
     
  16. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    Never place anything too close to the corners?

    upload_2021-5-16_3-1-25.png

    But if you really wanted to, you could have code that detects the pixel phone and override safeArea with your own values.

    But it generally seems like all Androids don't really inset because of rounded corners.

    Even Apple don't think does it because of rounded corners, they just do it so there's symmetry. (I'm fairly sure if they got rid of the notch but kept the rounded corners, the safeArea would be full screen).
     
  17. RLin

    RLin

    Joined:
    May 10, 2015
    Posts:
    133
    Well the point is I'd like the UI to be consistent between Android and iOS. If a button is pushed away from the screen edge on iOS due to a rounded corner, it would look strange to have that same button against the edge on Android.

    I am aware that Screen.safeArea is reported by the device and that Android handles corners differently; regardless, Android also has an additional RoundedCorner API that I want to access in Unity to compute my own custom safe area. So the question is, how to access it in Unity?
     
  18. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    It’s not due to a rounded corner, Apple did it this way to keep it symmetrical, which you could do by calculated the side offsets and applying the highest one for both sides.

    Also about the rounded corners API is something you could probably write a plugin for. Don’t expect Unity to do it for you.
     
  19. RLin

    RLin

    Joined:
    May 10, 2015
    Posts:
    133
    That's definitely not true. If you go to the safe area visualizer for the iPhone X, you'll see that the border is significantly larger than the notch in order to fully exclude the rounded corners:
    upload_2021-7-2_23-1-19.png
    The safe area shape exactly matches the rounded corners. If it was only to match the notch size, you'd expect the safe area to partially cut into the rounded corners.

    As a matter of fact, if you simulate the iPad Pro 11, which doesn't have any notches but does have rounded corners, you'll see that the safe area cuts out the corners:
    upload_2021-7-2_23-11-2.png
    This same cut does not happen on the iPad Pro 10, which has all regular corners.

    For an issue so common that someone started an entire thread about it with multiple people commenting wondering why the corners aren't considered, I'd want to at least make sure that Unity didn't already implement it before going out of my way to write a plugin. If I have to write a plugin, I'll do it, but it seems strange that a common issue doesn't have any built-in support.
     
  20. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    Welcome to Unity.
     
  21. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    I’m pretty sure that is not space for the rounded corners, just for the virtual home button. (Otherwise why is there no safe area on the top side as well?)
    (also has a physical button, so no need to leave space for the virtual home button)
     
    Last edited: Jul 3, 2021
  22. Deleted User

    Deleted User

    Guest

    Safe area really is bugged for these devices.
    The problem is also dependent on the System Navigation on the device.

    If you use 3-button navigation, the safe area is wrong.

    If you use Gesture navigation the safe area is correct.

    upload_2023-4-14_15-48-26.png
     
  23. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,828
    Are you talking on an actual device, or Unity's device simulator?
     
  24. Deleted User

    Deleted User

    Guest

    I was using a emulator from android Studio.

    But I was wrong, when you set the navigation to gesture navigation, it appears the OS itself cuts the app area.
    With the 3 button it doesn't, but the safearea value is returned correctly (it was my app that was wrong, a piece of canvas was missing the safe area script). Sorry about that, it was my mistake.

    Weird though that android cuts the app area when set to gesture navigation, it's either android or unity doing this automatically, the game gets a black bar under the camera.