1

I'm trying to make a script that adjusts the SafeArea of my Canvas when the device is rotated. I've got it almost working and it looks great on-launch, but I'm getting a weird result after rotating that I can't understand: the SafeArea is different depending on whether the app launched in Portrait or Landscape mode.

When I log out the value of Screen.safeArea on an iPhone 12 Pro, for example, I get the following values:

  • Portrait on launch: (x:0.00, y:102.00, width:1170.00, height:2289.00)
  • Portrait rotated to Landscape: (x:0.00, y:102.00, width:2532.00, height:927.00)
  • Landscape on launch: (x:141.00, y:63.00, width:2250.00, height:1107.00)
  • Landscape rotated to Portrait: (x:141.00, y:63.00, width:888.00, height:2469.00)

The Width/Height values are changing on rotation, which makes sense, but the X/Y offsets are not. Given the particular nature of the iPhone 12, that means there is a big empty space at the top when launching in Portrait and rotating to Landscape but overflowing the notch to the side, and big empty spaces on both sides when launching in Landscape and rotating to Portrait but overflowing the notch to the top.

Is this the expected behavior? I would have thought the "launching in Portrait" and "rotated to Portrait" safeAreas should be the same, right? Is there a way to force-refresh the safeArea or something, to get the proper X/Y values? Or am I misunderstanding how this is supposed to work?

Nerrolken
  • 1,975
  • 3
  • 24
  • 53
  • I'm not sure if it still is, but apparently this is a [`Unity Bug`](https://forum.unity.com/threads/canvashelper-resizes-a-recttransform-to-iphone-xs-safe-area.521107/page-2#post-5258726). Mentioned a few times in this post. – TEEBQNE Sep 23 '21 at 20:42

1 Answers1

1

As I mentioned in my comment, I believe this is a bug in the version of Unity you are using. Good news though! According to this bug report, it has been fixed in 2020.1.X, 2018.4.X and 2019.3.X. If you are already in one of these versions, it could be that the bug has returned. If that's the case, I would try one of the alternate versions listed. If no listed versions work, you should report that the bug has regressed.

TEEBQNE
  • 6,104
  • 3
  • 20
  • 37
  • 1
    I'm working in 2020.3.4, so it sounds like it's back. That's frustrating, but thanks for the heads up! – Nerrolken Sep 23 '21 at 20:55
  • @Nerrolken If you look at the comments of the bug report another user talks about it being an issue in 2020. They seem to have some form of a workaround / solution. – TEEBQNE Sep 23 '21 at 21:18
  • The work-around posted in the bug report seems to be misunderstanding where that user was transforming the safe area into the UGUI canvas. Whereas in this case, I believe @Nerrolken is talking about the values of the Screen.safeArea directly. – Stu Dec 09 '21 at 22:33