Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Android Auto rotation when locked

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

  1. lilboylost

    lilboylost

    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
     
    unity_O8QA1VNL9-VDmg likes this.
  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. lilboylost

    lilboylost

    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. niceplay_games

    niceplay_games

    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.
     
  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,396
    This was super handy, thanks for the tip!
     
  7. DimaHubenkoGamepoint

    DimaHubenkoGamepoint

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