I’m trying to work with screen rotations in our app. What I’m trying to achieve is this:
When “entering a page” (prefab is loaded), I’m setting the allowed autorotateToX values according to that page’s settings and I expect the screen to rotate to be within the allowed rotations.
For this example let’s say the device is in portrait mode currently, and I then set it to allow landscape left and right only.
Just setting the allowed rotations does not force the screen to rotate. If you then rotate the device to landscape, the screen orientation rotates as it should, after which point the restrictions seem to kick in, and you can no longer go back to portrait.
To get the screen to rotate to respect the allowed autorotations, the first thing I tried was setting the current orientation to auto after setting the allowed autorotations:
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.AutoRotation;
This had no effect.
Then, as further workaround/test, I tried setting the current orientation to LandscapeLeft, wait 3s and then set it to Auto:
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.LandscapeLeft;
await UniTask.Delay(3000);
Screen.orientation = ScreenOrientation.AutoRotation;
This caused the screen to rotate to LandscapeLeft as expected, but after the 3 seconds, the AutoRotation rotated it back to portrait. (The real physical rotation was portrait at all times)
Again, if I manually rotated the real device to landscape, I could no longer go back to portrait. i.e. It then respected the allowed rotation values.
So in conclusion, AutoRotation seems to rotate to the real physical rotation ignoring autorotateToX values, until the physical real orientation change triggers something, after which it respects the autorotateToX values.
Any ideas?
Edit:
I also reproduced the problem in an empty scene with a single canvas, button and script, just to rule out outside interference by my other scripts.
Also, I should mention that the problem happens on a real device (Honor 8, Android 7.0), but not on the Unity Device simulator in Play mode.