-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b91628
commit 2c72193
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |