diff --git a/Assets/Scripts/ScreenRotationManager.cs b/Assets/Scripts/ScreenRotationManager.cs new file mode 100644 index 000000000..450db1f57 --- /dev/null +++ b/Assets/Scripts/ScreenRotationManager.cs @@ -0,0 +1,33 @@ +using UnityEngine; + +public class ScreenRotationManager : MonoBehaviour +{ + void OnApplicationFocus(bool haveFocus) + { + if (haveFocus) + ToggleAutoRotation(); + } + + static void ToggleAutoRotation() + { + AutoRotationOn = DeviceAutoRotationIsOn(); + Screen.autorotateToPortrait = AutoRotationOn; + Screen.autorotateToPortraitUpsideDown = AutoRotationOn; + Screen.autorotateToLandscapeLeft = AutoRotationOn; + Screen.autorotateToLandscapeRight = AutoRotationOn; + Screen.orientation = ScreenOrientation.AutoRotation; + } + + static bool DeviceAutoRotationIsOn() + { +#if UNITY_ANDROID && !UNITY_EDITOR + using (AndroidJavaClass actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { + AndroidJavaObject context = actClass.GetStatic("currentActivity"); + AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings$System"); + int rotationOn = systemGlobal.CallStatic("getInt", context.Call("getContentResolver"), "accelerometer_rotation"); + return rotationOn==1; + } +#endif + return true; + } +}