From 2c721931cd5c454237111a2f65e4642e22417693 Mon Sep 17 00:00:00 2001 From: David Finol Date: Wed, 7 Feb 2018 11:20:55 -0600 Subject: [PATCH] Create ScreenRotationManager.cs --- Assets/Scripts/ScreenRotationManager.cs | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Assets/Scripts/ScreenRotationManager.cs 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; + } +}