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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Screen.safeArea rect not valid in Awake

Discussion in 'Android' started by Piflik, Mar 27, 2020.

Thread Status:
Not open for further replies.
  1. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    I am using Screen.safeArea in Awake() to determine the available resolution on the device, but recently it stopped working. Instead of returning a valid rect, the rect's width and height are NaN.

    Some information:
    • The application has been in use in my company for years.
    • I added Screen.safeArea last November.
    • The application worked fine this February
    • I have a build from late last November, after I added Screen.safeArea, that still works, but the next one, from early January, doesn't
    • When I go back in our Version Control to when I added Screen.safeArea, it does not work (in the current Version of Unity).
    • I have builds dating from early January till now, which all don't work, but I know for a fact that a build I made mid February did work back then
    Has there been any changes to Unity and/or the Android API that broke this function? I can use it later and get useful values, but in Awake it returns NaN, which it didn't before.
     
    alex_roboto likes this.
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    What is current version? LTS? Or are you using 2019.3?

    Screen safearea was broken in awake in the past, they then fixed it, but as most Unity issues do, it keeps coming back it seems.
     
  3. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    safe area NaNs are already fixed in 2020.1.0b2, backport to 2019.3 is on it's way also.
     
  4. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Is there an ETA on a fix in 2019.3 or a stable 2020 release?
     
    ajon542 likes this.
  5. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    It will be in 2019.3.9f1, should be out in 1-2 weeks
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Thank you.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    Is this fix backported to 2018.4 LTS? I think we also got this issue and we're using 2018.4.23f1 (latest LTS). Is there a workaround? Is reading safe areas outside of awake/start safer?

    Currently we are reading it in Start of the first scene and it's causing problems. Should we read it later?
     
    Last edited: May 21, 2020
  8. ajon542

    ajon542

    Joined:
    Oct 22, 2013
    Posts:
    25
    We are using 2018.4.23f1 and ran into this issue also. After some time debugging, we found that it seemed to be in some way related to the splash screen scaling setting.
    https://docs.unity3d.com/Manual/AndroidMobileCustomizeSplashScreen.html

    If either of "Center (only scale down)" or "Scale to fit (letter-boxed)" was used, the safe area rect would have NaN. If we changed to "Scale to fit (Cropped)", the safe area rect appears to be correct.
     
    AcidArrow likes this.
  9. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    Thanks for the information. I will do some investigation of my own.

    But you know what would be nice? If Unity had these things figured out so thousands of indie devs didn’t have to struggle with these issues and workarounds.
     
  10. ajon542

    ajon542

    Joined:
    Oct 22, 2013
    Posts:
    25
    Hmm, I spoke too soon. After testing a couple other Android devices, it's only resolved on one of them. Back to the drawing board.
     
    AcidArrow likes this.
  11. ajon542

    ajon542

    Joined:
    Oct 22, 2013
    Posts:
    25
    Hi rjonaitis, do you have a date for when this fix will be merged into 2018 LTS?
     
  12. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    I've created a backport to 2018LTS, it has to pass QA and other checks, so I can't give you an esimate when it will get into release.

    Until then, the best option for 2018LTS is to get safeArea in Update(). The reason for NaNs on some devices in Start() or Awake() is that Android's callback about window insets change can get delayed and be processed after scripting has already passed Start().
     
    ajon542 and AcidArrow like this.
  13. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    If that could still be a problem in the future, maybe a note about this in the docs could be prudent?

    We now have a solution that calculates the safe areas (we convert the numbers to viewport coords), 1 second after the 1 1st scene loads, and we also check for float.IsNaN (and if that's the case, we inset slightly so we have something that would look reasonable with and without a notch), and it kinda took us a lot of guesswork and time to arrive at this conclusion.

    Having something in the docs that it may be safer to read safeAreas a bit later would have insta solved our issues. (and I I mean that even after you fix the issue, because then Android again may change something that yet again makes it unsafe to read these values in Awake/Start, right?)
     
    Last edited: Aug 27, 2020
  14. ajon542

    ajon542

    Joined:
    Oct 22, 2013
    Posts:
    25
    Just a heads up, this is still an issue in 2018.4.24
     
  15. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    The backport got into 2018.4.25
     
    ajon542 likes this.
  16. andreasp

    andreasp

    Joined:
    Jun 29, 2013
    Posts:
    14
    @rjonaitis What about 2019.4, has it been ported there? I can't see a specific item on the 2018.4.25 release notes that addresses this so don't know what to look for in 2019.4 release notes either.
     
  17. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Yes, it's already in 2019.4
     
  18. alex_roboto

    alex_roboto

    Joined:
    Nov 11, 2019
    Posts:
    24
    I know you were probably writing short hand, but float.isNaN() is how to detect NaN. Comparing anything to float.NaN using == returns false always.
     
    AcidArrow likes this.
  19. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    You are right, I just edited the original post so I don't spread wrong information.
     
    alex_roboto likes this.
  20. Inboxninja

    Inboxninja

    Joined:
    Feb 19, 2014
    Posts:
    14
    This appears to still be an issue in 2019.4.9f1 LTS. Except, now it returns the full screen size, rather than the safe screen size.
     
    Last edited: Sep 6, 2020
  21. Numa

    Numa

    Joined:
    Oct 7, 2014
    Posts:
    100
    This is still happening in 2020.1.12f1, it returns the full screen size

    Side question, when can we expect the iphone 12 series to be added to the device generation enum? (also, Device.generation.ToString() still returns "iPhoneUnknown" at the moment).
     
    Last edited: Nov 9, 2020
  22. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    Have you tried reading it a little later (not in awake in the first scene) and see if it returns correct stuff?
     
  23. wanted748

    wanted748

    Joined:
    Mar 11, 2018
    Posts:
    11
    Still happens in Unity 2021.3.14f1. Safe area is the same as original resolution when called on Awake()
     
  24. michelex2005

    michelex2005

    Joined:
    Aug 17, 2019
    Posts:
    12
    Just a little heads up: Unity 2022.3.4f1 LTS brought this back (it wasn't there in previous 2022 versions)
     
  25. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    Welp, I guess this usually means it will be backported to 2021 soon as well.
     
Thread Status:
Not open for further replies.