Skip to content

Commit

Permalink
Create ScreenRotationManager.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol authored Feb 7, 2018
1 parent 4b91628 commit 2c72193
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Assets/Scripts/ScreenRotationManager.cs
Original file line number Diff line number Diff line change
@@ -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<AndroidJavaObject>("currentActivity");
AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings$System");
int rotationOn = systemGlobal.CallStatic<int>("getInt", context.Call<AndroidJavaObject>("getContentResolver"), "accelerometer_rotation");
return rotationOn==1;
}
#endif
return true;
}
}

0 comments on commit 2c72193

Please sign in to comment.