Search Unity

Support for Screen.safeArea on notch iPhone X like Android devices

Discussion in 'Android' started by jason_yak, Jul 3, 2018.

  1. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    @mtalbott are you talking of more device-alike layouts in the game view, including notch?
     
  2. mtalbott

    mtalbott

    Joined:
    Dec 21, 2011
    Posts:
    125
    @Yury-Habets, visually seeing the notch would be nice but not exactly what I was requesting.

    To be clearer: Our issue/request is to better/more accurately visually what our app will look like on a given device. Currently, we always use a CanvasScaler for our ui layouts that adjusts the canvas scale based on Screen.dpi. Additionally, we have a custom UI LayoutElement which constraints a RectTransform to the Screen.SafeArea. It would be nice to be able to have a dpi and safe area associated for each Game-View Preset so that my UI canvas' would accurately represent what I will see on the device.
     
  3. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    I tried to build a test app (using Unity 2018.3) on some Android Devices with notch, and in all of these the values of Screen.safeArea changes if I read it inside Start() or after some seconds.

    These values, for example, are taken from Samsung Galaxy S10 (Android 9 Pie 1440 x 3040 px):
    At start:

    Screen.height = 2890
    Screen.safeArea.height = 2890
    Screen.safeArea.y = 0

    After some seconds:

    Screen.height = 3040
    Screen.safeArea.height = 2891
    Screen.safeArea.y = 149 (This is probably the space occupied by the front camera on top)

    There are 2 problems for me:
    - 1st is that I cannot read the values at start, because the notch area is read after a while.
    - 2nd is that Screen.safeArea.y in my iPhone10 returns the space from the BOTTOM of the screen to the BOTTOM of the safe area, instead on the Android devices I tested, Screen.safeArea.y returns the space from the TOP of the screen to the TOP of the safe area.

    Too many variants, the risk is a mess in my UI!
     
  4. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    The 1st problem: safeArea is being reported by the Android OS, what is happening during the start, when the application is lauched the android's navigation bar is still being displayed for a moment, thus reporting safeArea accounting the navigation bar, after it dissapears android updates it again. This could probably be solved by editing activity style to have transparent navigation bar during startup.

    The 2nd problem: the bug where android's Screen.safeArea in 2018.3 is reported upside down is already known and has a backport ready (due to low priority it's been delayed).
     
    geganadiradze and areavisuale like this.
  5. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    This can be done with manifest.xml? I cannot find a reference, can you help me?
     
  6. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Create a file Assets/Plugins/Android/res/values-v28/styles.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="BaseUnityTheme" parent="android:Theme.Holo.Light.NoActionBar.Fullscreen">
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
    </resources>

    This should solve your problem of changing safeArea during start.
     
  7. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    This works, thanks!
    There are some Android 7.1.1, 8.0, and 8.1 devices that have notch. Can I use the same code putting it in values-v25 folder?
     
  8. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
  9. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    I'm using Unity 2019.1.0f2 and I enabled "Render outside safe area" in playerprefs.
    If I test the apk in an Android 9 emulator with the cutout option enabled in the developer settings all works as it should be.

    But I have done a test with an Asus Zenfone Pro M2 (Android 8.1, screen 2280px x 1080px with notch):
    7bda2951-8ef9-4853-8de3-c6d979a5f732.jpg
    The white background should render next to the notch. The text fields says (inside Start() and inside Update()):
    Screen.height: 2199px
    Screen.safeArea.height: 2199px

    but the screen height should be 2280px. It works as if I had not enabled the Render outside safe area" option.

    I know that the safearea is officially supported in Android 9, and some Android 8 devices could not be supported, but some apps, like Google Maps has the backround rendered next to the nocth, see the image:
    ad9b0fce-9143-4576-ad70-c891d6fa0f7c.jpg

    Normally I read the safearea value to determine if there's a notch, and in this case show the status bar on top superimposed on the background.

    Any clues?
     
  10. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Notch support on Android 8 is entirely dependent on manufacturer. Some have available settings that can be enabled in app's manifest, other's have some kind of list of applications in the OS display settings that should force to whole screen. Some others expect that app has to declare it's maximum aspect ratio large enough to allow filling the whole screen https://stackoverflow.com/a/48659699.
     
  11. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    One minor correction:
    For API 28, it should be Material theme instead of Holo.
     
  12. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    AcidArrow likes this.
  13. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    Why?
     
  14. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Because for API23+ Google Play is expecting Material-based themes. You'll likely to get a warning when submitting your APK otherwise.
     
  15. manel_besoccer

    manel_besoccer

    Joined:
    Jul 13, 2018
    Posts:
    3
    Not working for me on a MI 8 Lite (MIUI Global 10.3). After 10 seconds of script initialization, the Screen.safeArea returns the same size than Screen:


    [SafeArea] SafeArea rect: (x:0.00, y:0.00, width:1080.00, height:2280.00)
    [SafeArea] Screen width: 1080. Screen height: 2280
     
  16. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    If it is running Android 8.1, it will not adjust screen safe area (Android 8.1 does not provide API to get the safe area).
     
  17. manel_besoccer

    manel_besoccer

    Joined:
    Jul 13, 2018
    Posts:
    3
    It's running Android 9 (PKQ1.181007.001), under MIUI Global 10.3
     
    marie-hmm likes this.
  18. prawn-star

    prawn-star

    Joined:
    Nov 21, 2012
    Posts:
    76
    Any one got any ideas on how to find the height of the screens rounded corners for devices like the Samsung S9
    I was hoping that Yury Habets UnityAndroidNotchSupport plugin on Gihub would do the trick.
    But the DisplayCutout returns null for this device.
     
  19. TheFellhuhn

    TheFellhuhn

    Joined:
    Feb 3, 2017
    Posts:
    42
    Still have black bars and wrong screen sizes on Unity 2018.4.11f1 on some devices. If I read this correctly all fixes have been backported to 2018.4, right?

    So it should be so that I only need to check "Draw outside safe areas" and everything is fine, right? Or do I still have to tweak the Manifest files for different devices, create my own Activity to set some layout options etc.?

    EDIT: The complaining customer has a Honor 10, running EMUI 9.1.0, with a 2280*1080 resolution. Before I updated to Unity 2018.4 and without the "Render outside safe areas" he had a black bar but everything else was where it belonged. After the update and enabling the option he still has the black bar but Unity renders behind it.
     
    Last edited: Oct 21, 2019
  20. CongDT7

    CongDT7

    Joined:
    Mar 19, 2020
    Posts:
    5
    Hello,
    On android, unity has the option Render outside safe area = false, there will be a black bar covering the cutout. I want to remove the notch and render the black bar same with android devices, but on ios, I don't see this option, I wonder if anyone has done this on ios yet? Give me a solution to it.
     
  21. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,716
    You might not pass Apple's review if you find a way to do this.
     
    Firemaw likes this.
  22. CongDT7

    CongDT7

    Joined:
    Mar 19, 2020
    Posts:
    5
    Sorry I dont understand what is your mean ?
     
  23. tz

    tz

    Joined:
    Oct 13, 2007
    Posts:
    91
    @rjonaitis, @Yury-Habets

    Hey guys, we're using Safe Area successfully on a large number of Android devices without problems.

    We are however, seeing a particular issue with some OnePlus and LG phones.

    I'm holding a OnePlus 6T running Android 10 right now, where the rendered part of the area is correctly pushed down below the notch (blacking the notch out), however the touchable area of the screen starts farther down, leaving some buttons at the top of the screen non-interactable.

    In other words, the renderable and interactable parts of the screen are not aligned on that particular device.

    We've gotten dozens of complaints about this (leading me to believe we probably have hundreds/thousands of people running into it).

    Have you heard/got any hints as to what might be going on here?
     
  24. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Hi, we haven't noticed such issue, please report a bug and attach a sample project that reproduces such behaviour. It sounds like those particular devices might be handling app window sizes differently than everyone else.
     
  25. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,716
    We've had situations where SafeAreas return NaNs. We were reading them in Start of the first scene.

    Any ideas for workaround? We know just read them at an arbitrary point later in the 1st scene and we also have a special case for NaNs, so we're "fine", but we'd like to reliably get proper values if that's possible.
     
  26. tz

    tz

    Joined:
    Oct 13, 2007
    Posts:
    91
    I didn't put up a project, but did end up tracking down the problem.

    It appears to be a bug with the "Filter touches while obscured" setting flag.

    Trivially easy to repro... make a new scene, check the flag on, and build to a OnePlus 6T.

    The top (almost) 1 cm of the rendered area will be inaccessible to touches.
     
  27. Lukas_Andriejunas

    Lukas_Andriejunas

    Unity Technologies

    Joined:
    Aug 19, 2016
    Posts:
    20
    @tz
    I created a new project, added a simple script to print out the touch position (https://docs.unity3d.com/ScriptReference/Touch-position.html), enabled a flag "Filter touches while obscured" and built it with unity 2020.2.0a15, run on OnePlus 6T (Android 9) and couldn't reproduce the issue.

    Could you please submit a bug report using a Bug Reporter and attach a project and .apk? I will try to track down if it's related to the Unity version or rather device OS.
     
  28. cadrick

    cadrick

    Joined:
    Jul 23, 2016
    Posts:
    9
    How can we disable Render Outside SafeArea for android older than 9.0?

    We're facing an issue where Android 8.0 devices rendering the UI above the notch.

    We want to allow Render Outside SafeArea for Android 9.0 and above but don't want to allow for Android 8.0 and older.

    Any workaround for this issue?
     
  29. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,896
  30. KonstantinMaximus

    KonstantinMaximus

    Joined:
    Feb 13, 2014
    Posts:
    21
    Is there any update on this? I tried using SDK 28-30 + 2019x, 2020x - rounded edges on pixel 4 are not detected by the safe area. Is there a work around this?
     
  31. BHGAhuerta

    BHGAhuerta

    Joined:
    Apr 4, 2018
    Posts:
    14
    I don't believe that will be considered out of the safe area. The Android design documents mention not placing any important UI elements in the corners of the display, so I would just make it a habit of following that.
     
  32. KonstantinMaximus

    KonstantinMaximus

    Joined:
    Feb 13, 2014
    Posts:
    21
    I think it's pretty common to place a settings or menu button in a corner of a screen. This could be a setting (ie avoid corners or not) Or Android should have a way to check that. I ended up floating my UI elements by 40px on each axis towards the center. Not a fan.
     
  33. Appyfurious

    Appyfurious

    Joined:
    Jul 11, 2019
    Posts:
    2
    Hi everyone. The problem has reappeared in Unity2020.2.1f1, on all android devices safeArea return 0. This is also reproduce in the editor + simulation
     
  34. IevaUnity

    IevaUnity

    Unity Technologies

    Joined:
    Dec 15, 2014
    Posts:
    6
    Hey. I have tested what safeArea returns and I don't get any issues. I have tested Unity 2020.2.1f1 ran on device simulator and actual device. Used Samsung Galaxy Note10 and received these values
    SafeArea (x:0.00, y:0.00, width:1080.00, height:2190.00) Screen.width 1080 Screen.height 2280
    Could you please report a bug and provide more details about the problem you mentioned in your comment?
     
    Last edited: Jan 26, 2021
    Yury-Habets likes this.
  35. mace44

    mace44

    Joined:
    Feb 8, 2017
    Posts:
    15
  36. trirom1

    trirom1

    Joined:
    Jun 5, 2014
    Posts:
    14
    Can someone help me how to set screensize like Unity 2017. All game content and ads are automatically in Safe Area without having to use script.
     

    Attached Files:

  37. CChong_at_Infinity

    CChong_at_Infinity

    Joined:
    Apr 7, 2020
    Posts:
    27
    I realise it's not a fail-safe solution but, on Android at least, one can assume that a device with an aspect ratio that's wider than 16:9 has a notch or camera hole.

    On such devices, you can sort of make an educated guess on how thick the notch area is and just symmetrically trim it on both edges.

    We're doing this symmetrical trimming when using safeArea anyway because some Android phones don't consider the curved corners on the non-notched edge, which end up clipping some of our UI.

    The user-feedback from this has been positive because the game's UI doesn't appear shifted to the left or right when playing in horizontal orientation, like it would be when only the notched edge is safe-area-ed.

    We're thinking of doing this on our project because there are a few Android phones out there which aren't reporting the correct safeArea.