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

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

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

  1. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    502
    Hi just wondering if there are any plans to support all of the android devices that replicate the notch and have different ‘safe area’ values. There’s a growing list of devices:

    https://www.theandroidsoul.com/android-phones-with-notch-like-iphone-10/amp/

    Or do you support some of these already? It would be handy if the docs specified which devices the Screen.safeArea returns values for.
     
    Last edited: Jul 3, 2018
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    The corresponding API from Google appears in Android P which has not been released yet. We'll add support for getting the safe area as soon as it is out. (there may be more related things coming later, like in-editor preview)
     
    Dalton-Lima, Nananaaa and Algerator like this.
  3. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    502
    Thanks, sounds good. Makes sense it’s Android API driven, so unity doesn’t have to define it per device but lets just hope manufacturers actually implement it properly haha... the preview would be great, we’ve created our own aplies and overlay for now.
     
    Yury-Habets likes this.
  4. Algerator

    Algerator

    Joined:
    Mar 9, 2015
    Posts:
    3
    Hi!
    Android P started to rollout 4 days ago on the Pixel phones.
    Any news on the issue? :)

    PS: The in-editor preview would be awesome as well ^^
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,001
    Just making sure, in devices where the API is not there, does Screen.safeAreas return 0,0,Screen.width, Screen.height? (so that it's safe to use everywhere, even if it doesn't do anything special)
     
  6. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Since Android 9 Pie is out - the support for safe area is in progress.
    @AcidArrow yes this sounds like an intended behavior to me.
     
    Algerator and AcidArrow like this.
  7. Valdeco

    Valdeco

    Joined:
    Sep 7, 2014
    Posts:
    11
    Any news on the Android safeArea support?
     
  8. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    It's in progress. :)
     
  9. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    383
    Would really like to get this rolled into 2017.4 LTS at the same time as 2018 versions, please.
     
  10. faizidev

    faizidev

    Joined:
    Aug 20, 2014
    Posts:
    4
    is anybody have any solution to add safe area on android notch phone?
     
  11. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
  12. robson_depaula

    robson_depaula

    Joined:
    Sep 30, 2014
    Posts:
    37
    Hello @FaiziFazal,

    I was interested in avoiding the vertical space occupied by the notch. In order to do that I created a minimal Android library to work as a native plugin for Unity and used:
    Code (CSharp):
    1.  
    2. Window unityWindow = UnityPlayer.currentActivity.getWindow();
    3. DisplayCutout displayCutout = unityWindow.getDecorView().getRootWindowInsets().getDisplayCutout();
    4. if (displayCutout != null) {
    5.        return displayCutout.getSafeInsetTop();
    6. }
    7.  
    On the Unity side I had to move a Canvas element, for that I had to use the scaler of the parent. Something like:
    Code (CSharp):
    1.  
    2. int pixels = DeviceHandler.GetCutOutSafeTop ();
    3. if (pixels != -1) {
    4.     Canvas parentCanvas = GetComponentInParent<Canvas> ();
    5.     RectTransform rectTransform = GetComponent<RectTransform> ();
    6.     rectTransform.localPosition += Vector3.down * (pixels / parentCanvas.scaleFactor);
    7. }
    8.  
    Hope that helps.
     
    Anh-Pham likes this.
  13. southrop

    southrop

    Joined:
    May 14, 2018
    Posts:
    4
  14. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    That's a great question.
    Android versions pre-P don't have a corresponding API, so we won't be able to return safe area on Androids earlier than 9.

    @rjonaitis can you please confirm?
     
  15. ApenasVB

    ApenasVB

    Joined:
    Nov 26, 2014
    Posts:
    3
    It would make sense that for devices pre-P, Screen.safeArea returned a rect of dimens (0, 0, Screen.width, Screen.height)
     
    Yury-Habets likes this.
  16. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Correct, notch support pre-P is entirely dependent on device manufacturer and it is optional. So there is no guarantee that device manufacturer will provide any solution for retrieving it's device safe area, therefore Screen.safeArea will return Rect(0, 0, Screen.width, Screen.height) when running on Android 8.1 and lower.
     
    AcidArrow likes this.
  17. BHGAhuerta

    BHGAhuerta

    Joined:
    Apr 4, 2018
    Posts:
    14
    Will support for safe areas in android 9 be coming to 2017.4 LTS?
     
  18. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Backporting notch support to 2017.4 is currently not planned.
    Currently 2017.4 Android Screen.safeArea always returns Rect(0, 0, Screen.width, Screen.height). When it is ran on Android 9 device with notch, the app screen is being letterboxed so the notch does not overlap application (essentialy resizing app screen to it's safeArea)
     
  19. BHGAhuerta

    BHGAhuerta

    Joined:
    Apr 4, 2018
    Posts:
    14
    From our testing on 2017.4.11f1 unity still registers touch input in the black bars. This doesn't seem like an ideal scenario.
     
  20. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    When letter boxed, touch input coordinates (0,0) match with the rendered screen (0,0). So you can filter undesired touch events by checking if they are within Screen.safeArea.
     
  21. itayganor

    itayganor

    Joined:
    Sep 24, 2018
    Posts:
    14
    How can I use it in my project?
     
  22. robson_depaula

    robson_depaula

    Joined:
    Sep 30, 2014
    Posts:
    37
  23. Kaiymu_

    Kaiymu_

    Joined:
    Jun 1, 2016
    Posts:
    47
    Hello
    Thanks for that ! I'm going to try your solution, and hopefilly it'll work !

    EDIT :

    I did that 2 month ago, I was not able to test before because notch was not out.
    Maybe we don't need a small library and can only do that on C# side.

    What do you think ?

    Code (CSharp):
    1. // We get the current window from the current activity
    2. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3. AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    4. AndroidJavaObject currentWindow = currentActivity.Call<AndroidJavaObject>("getWindow");
    5.  
    6. // Get the current view and inset
    7. var currentView = currentWindow.Call<AndroidJavaObject>("getCurrentFocus");
    8. var currentWindowInset = currentView.Call<AndroidJavaObject>("getRootWindowInsets");
    9.        
    10. // Should return something, but because the Android Pixel 3 has no "physical" notch, seems to return ... Nothing.
    11. var displayCutout = currentWindowInset.Call<AndroidJavaObject>("getDisplayCutout");"
     
  24. robson_depaula

    robson_depaula

    Joined:
    Sep 30, 2014
    Posts:
    37
    Hi @Kaiymu_ ,

    I personally do not like to write Android plugins directly in C# for the sake of modularity, dependency management and testability.

    If this is fine for you, go for it :)
     
  25. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Will the Screen.safeArea on android device support in 2018.3?
     
  26. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Yes
     
  27. imaginalis

    imaginalis

    Joined:
    Feb 20, 2015
    Posts:
    29
    Is Screen.safeArea already implemented for Android?
    Will it be included in 2018.2?
     
  28. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Yes, it's already implemented for android in 2018.3 and up. It will not be backported to 2018.2.
     
    Yury-Habets likes this.
  29. Oscar-Tsang

    Oscar-Tsang

    Joined:
    Nov 7, 2012
    Posts:
    78
    Can it add on 2017.4?
     
  30. Zekemyapp

    Zekemyapp

    Joined:
    Mar 15, 2015
    Posts:
    2
    I'm still getting the black bar on notch area after activating the "render outside safe area" option (2018.3). Am I missing some configuration? :( I'm testing on a Pixel 3XL.
     
  31. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,001
    Submit a bug report?

    AFAIK there isn't another requirement.
     
  32. umurox

    umurox

    Joined:
    Mar 1, 2017
    Posts:
    8
    How can we use that code? I use Xiaomi Mi 8 and it didn't work for me
     
  33. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    What is your target API level?
     
  34. BigDuckGames

    BigDuckGames

    Joined:
    Nov 3, 2016
    Posts:
    2
    I'm seeing the same thing. Also still getting a black bar on the notch area of a Pixel 3 XL, even after setting "Render outside safe area", using Unity 2018.3.4f1, and target API level 28.
     
    AcidArrow, N1kel and itayganor like this.
  35. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,001
    Please submit a bug report, this sounds serious, it seems like "Render outside safe area" does nothing.

    (I would do a bug report myself, but I don't have an android with a notch handy)
     
  36. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Yury-Habets Is there a reason Android's Player Settings has 'Render outside safe area', which we use, but iOS Player Settings does not have this same option? It seems like this would be very helpful to have. Because of this I have to write special logic just for iOS.
     
    NEVER-SETTLE and N1kel like this.
  37. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    iOS does not provide settings that would allow to letterbox the app, the app would be letterboxed only if it is built with old xcode version, which did not had notch support.
     
    AcidArrow likes this.
  38. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I see. So, the only possible approach is to write out own system with Screen.safeArea? Or does that not work on iOS as well?

    If Screen.safeArea does work on iOS, wouldn't the Unity devs be able to use that to add a checkbox for iOS in PlayerSettings or no?
     
  39. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Screen.safeArea does work on iOS, so yes you should use it to adjust your UI or other elements according to it.
    As there is no support from the OS for letter boxing, it would have to be faked, and there is no single correct way how to do it best without appropriate performance/complexity costs, so it's not gonna be implemented in Unity. Plus the iPhone safe area size changes depending on device orientation. So it's up to the game developer to handle it as only he has all the information of what's going to be in the app and how it should be handled.
     
  40. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Ok. As long as Screen.safeArea works on iOS, that's enough to get started. Thanks for the explanation.
     
  41. BigDuckGames

    BigDuckGames

    Joined:
    Nov 3, 2016
    Posts:
    2
    I'm uploading a bug report now for the issue reported above where "Render outside safe area" does not work as expected. I also discovered that the issue is related to the Orientation->Default Orientation setting. "Render outside safe area" appears to only work when the Unity default Orientation value of "Auto Rotation" is selected. Our game is set to Portrait and it doesn't work. If I instead select Auto Rotation, and within the allowed orientation list select only Portrait, then it does work. Not sure whether there are any other behavior changes when switching from Portrait to Auto Rotation (Portrait), so I'm reluctant to change this just to workaround the notch issue. Hopefully Unity can fix this.
     
  42. geganadiradze

    geganadiradze

    Joined:
    Sep 23, 2017
    Posts:
    8
    Thanks for report and information :) It works in same fashion for me, so if I choose auto rotation+select portrait it works fine. BTW Unity's splash screen only uses safe area, but I guess that's not a bug but expected behavior.
     
  43. NEVER-SETTLE

    NEVER-SETTLE

    Joined:
    Apr 22, 2018
    Posts:
    30
    I heard u guys like notches

     
  44. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Is there any progress on this issue?
    I test the 2018.3 and it still not correct on android device.
     
  45. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    824
    This feature is already implemented in 2018.3. Make sure you use the latest available version. Please report a bug if you find issues with this feature.
     
  46. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Is it possible to use the Camera.pixelRect to have the camera only render to the Screen.safeAre?
     
  47. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Yes, you can change the camera rect to match the Screen.safeArea. But I suspect there is something more that you are expecting to achieve by doing this?
     
  48. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I wanted to move the camera rect inwards to prevent our game from rendering under the iPhone X's notch. The only way I can think to do that is set its rect to the Screen.safeArea. But when I first tried this, it did not seem to work.
     
  49. btyson_z

    btyson_z

    Joined:
    Aug 10, 2018
    Posts:
    9
    This is bad news for many live projects, particularly at larger studios. Thanks a lot.
     
  50. mtalbott

    mtalbott

    Joined:
    Dec 21, 2011
    Posts:
    125
    @rjonaitis @JuliusM. Any chance you all (UT) could implement Screen.safeArea and Screen.dpi into the the game-view pre-defined resolutions. Currently, there is not good way to actually test UI layouts for several mobile devices in the editor without creating complicated editor tools.