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,072
    @mtalbott are you talking of more device-alike layouts in the game view, including notch?
     
  2. mtalbott

    mtalbott

    Joined:
    Dec 21, 2011
    Posts:
    108
    @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:
    44
    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:
    46
    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:
    44
    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:
    46
    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:
    44
    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:
    46
  9. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    44
    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:
    46
    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,072
    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,072
    AcidArrow likes this.
  13. areavisuale

    areavisuale

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

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,072
    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:
    46
    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
     
  18. prawn-star

    prawn-star

    Joined:
    Nov 21, 2012
    Posts:
    40
    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:
    7
    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