Search Unity

Question ScreenOrientation.AutoRotation ignores autorotateToX values

Discussion in 'Android' started by OneNot, Apr 25, 2021.

  1. OneNot

    OneNot

    Joined:
    Jan 26, 2018
    Posts:
    4
    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:
    Code (CSharp):
    1. Screen.autorotateToLandscapeLeft = true;
    2. Screen.autorotateToLandscapeRight = true;
    3. Screen.autorotateToPortrait = false;
    4. Screen.autorotateToPortraitUpsideDown = false;
    5. 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:
    Code (CSharp):
    1. Screen.autorotateToLandscapeLeft = true;
    2. Screen.autorotateToLandscapeRight = true;
    3. Screen.autorotateToPortrait = false;
    4. Screen.autorotateToPortraitUpsideDown = false;
    5. Screen.orientation = ScreenOrientation.LandscapeLeft;
    6. await UniTask.Delay(3000);
    7. 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.
     
    Last edited: Apr 25, 2021
  2. c0ffeeartc

    c0ffeeartc

    Joined:
    Jul 23, 2015
    Posts:
    42

    I confirm this bug for Unity 2020.3.9f1 Android Huawei P30.
    There are multiple bug reports about this for different unity versions in Postponed state.

    Workaround is running custom AutoRotate function in LateUpdate, that checks Input.deviceOrientation, Screen.orientation, Screen.autorotateToX values and sets Screen.orientation to correct value.
     
  3. Mayis

    Mayis

    Joined:
    Feb 1, 2014
    Posts:
    6
    I confirm this bug for Unity 2020.3.12f1 LTS Android.

    Any updates or solutions?
     
  4. davidro_unity

    davidro_unity

    Unity Technologies

    Joined:
    Apr 18, 2019
    Posts:
    22
  5. sl1nk3_ubi

    sl1nk3_ubi

    Joined:
    Aug 21, 2019
    Posts:
    16
    Sorry to necro this thread, but was this really fixed? I can repro the exact same issue on 2022.3.19f1 is there something specific to do to force the orientation to the current settings?
    Everything works as expected on iOS

    I should note I tested it with both User and Sensor auto rotation behaviors, both have the same issue
     
    Last edited: Apr 5, 2024