Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android Auto rotation when locked

Discussion in 'UGUI & TextMesh Pro' started by mog-mog-mog, Nov 15, 2014.

  1. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    On Android device,Unity auto-rotation overrides user's device "Auto-rotation" setting while it respect user setting on IOS device. Is this a bug on Android or expected?

    Unity game with Screen.orientation = AutoRotation
    1) Android device, screen auto-rotation: Locked
    Game still auto-rotate
    2) IOS device, screen auto-rotation: Locked
    Game does not auto-rotate
    3) IOS device, screen auto-rotation: Unlocked
    Game allow auto-rotate
     
  2. jongallant

    jongallant

    Joined:
    Feb 12, 2014
    Posts:
    4
    I've been trying to find a work around for this bug. Have you figured out how to get Unity to respect android system settings?

    I do not understand why this is not a higher profile bug.
     
  3. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    You can read device setting - Auto rotation on/off using Native code and change your code. I would like Unity team to comment what is expected behaviour..
     
  4. danmerey

    danmerey

    Joined:
    Jun 25, 2015
    Posts:
    14
    Didn't find any good solution, so I've made my own one (used knowledge from this post).

    Code (CSharp):
    1. void OnApplicationFocus(bool haveFocus)
    2. {
    3.     if (haveFocus) GameScreen.ToggleAutoRotation();
    4. }
    5.  
    6. static void ToggleAutoRotation()
    7. {
    8.     AutoRotationOn = DeviceAutoRotationIsOn();
    9.     Screen.autorotateToPortrait = AutoRotationOn;
    10.     Screen.autorotateToPortraitUpsideDown = AutoRotationOn;
    11.     Screen.autorotateToLandscapeLeft = AutoRotationOn;
    12.     Screen.autorotateToLandscapeRight = AutoRotationOn;
    13.     Screen.orientation = ScreenOrientation.AutoRotation;
    14. }
    15.  
    16. static bool DeviceAutoRotationIsOn()
    17. {
    18.     #if UNITY_ANDROID && !UNITY_EDITOR
    19.     using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    20.         {
    21.             var context = actClass.GetStatic<AndroidJavaObject>("currentActivity");
    22.             AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings$System");
    23.             var rotationOn = systemGlobal.CallStatic<int>("getInt", context.Call<AndroidJavaObject>("getContentResolver"), "accelerometer_rotation");
    24.  
    25.             return rotationOn==1;
    26.         }
    27.         #endif
    28.         return true;
    29. }
    What we do here:
    • Every time application gets focus, we check if Auto Rotation on or not in android settings.
    • If Auto Rotation is off, disable every auto rotation orientation, so screen stuck in current orientation.
    • If Auto Rotation is on, enable all rotation orientations for auto rotation.
    Works perfectly! Hope, it will help developers. And I also hope, Unity Team will add fix in engine in nearest future.
     
    camillazi, Mykaelos2, ltomov and 10 others like this.
  5. SioHio

    SioHio

    Joined:
    Oct 3, 2015
    Posts:
    6
    This bug still exists in 2019 o_O
     
    EZaca and herb_nice like this.
  6. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    This was super handy, thanks for the tip!
     
  7. DimaHubenkoGamepoint

    DimaHubenkoGamepoint

    Joined:
    May 16, 2018
    Posts:
    20
    Still present in 2019.4.10f1. Anyone from Unity going to fix this?
     
  8. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    565
    Unbelievable
     
  9. eldelnacho

    eldelnacho

    Joined:
    May 19, 2021
    Posts:
    2
    Apologies for the noob reply. But how am I supposed to implement this code in Unity?
     
  10. Francesco-FL

    Francesco-FL

    Joined:
    May 25, 2021
    Posts:
    176
    DankalApps likes this.