Created
February 8, 2018 22:14
-
-
Save yasirkula/e056df5e7af1ce2de98c6372d0f26ade to your computer and use it in GitHub Desktop.
Easily enable/disable auto screen orientation in Unity and receive callback upon orientation change. See the comments section below for instructions.
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
using UnityEngine; | |
public class DeviceOrientationManager : MonoBehaviour | |
{ | |
private const float ORIENTATION_CHECK_INTERVAL = 0.5f; | |
private float nextOrientationCheckTime; | |
private static ScreenOrientation m_currentOrientation; | |
public static ScreenOrientation CurrentOrientation | |
{ | |
get | |
{ | |
return m_currentOrientation; | |
} | |
private set | |
{ | |
if( m_currentOrientation != value ) | |
{ | |
m_currentOrientation = value; | |
Screen.orientation = value; | |
if( OnScreenOrientationChanged != null ) | |
OnScreenOrientationChanged( value ); | |
} | |
} | |
} | |
public static bool AutoRotateScreen = true; | |
public static event System.Action<ScreenOrientation> OnScreenOrientationChanged = null; | |
[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.AfterSceneLoad )] | |
private static void Init() | |
{ | |
DontDestroyOnLoad( new GameObject( "DeviceOrientationManager", typeof( DeviceOrientationManager ) ) ); | |
} | |
void Awake() | |
{ | |
m_currentOrientation = Screen.orientation; | |
nextOrientationCheckTime = Time.realtimeSinceStartup + 1f; | |
} | |
void Update() | |
{ | |
if( !AutoRotateScreen ) | |
return; | |
if( Time.realtimeSinceStartup >= nextOrientationCheckTime ) | |
{ | |
DeviceOrientation orientation = Input.deviceOrientation; | |
if( orientation == DeviceOrientation.Portrait || orientation == DeviceOrientation.PortraitUpsideDown || | |
orientation == DeviceOrientation.LandscapeLeft || orientation == DeviceOrientation.LandscapeRight ) | |
{ | |
if( orientation == DeviceOrientation.LandscapeLeft ) | |
{ | |
if( Screen.autorotateToLandscapeLeft ) | |
CurrentOrientation = ScreenOrientation.LandscapeLeft; | |
} | |
else if( orientation == DeviceOrientation.LandscapeRight ) | |
{ | |
if( Screen.autorotateToLandscapeRight ) | |
CurrentOrientation = ScreenOrientation.LandscapeRight; | |
} | |
else if( orientation == DeviceOrientation.PortraitUpsideDown ) | |
{ | |
if( Screen.autorotateToPortraitUpsideDown ) | |
CurrentOrientation = ScreenOrientation.PortraitUpsideDown; | |
} | |
else | |
{ | |
if( Screen.autorotateToPortrait ) | |
CurrentOrientation = ScreenOrientation.Portrait; | |
} | |
} | |
nextOrientationCheckTime = Time.realtimeSinceStartup + ORIENTATION_CHECK_INTERVAL; | |
} | |
} | |
public static void ForceOrientation( ScreenOrientation orientation ) | |
{ | |
if( orientation == ScreenOrientation.AutoRotation ) | |
AutoRotateScreen = true; | |
else if( orientation != ScreenOrientation.Unknown ) | |
{ | |
AutoRotateScreen = false; | |
CurrentOrientation = orientation; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Device Orientation Manager for Unity 3D
This script helps you toggle auto screen orientation for mobile devices in Unity easily, in addition to providing a callback for screen orientation change.
Don't add this component to an object manually, it is initialized automatically!
How To
DeviceOrientationManager.AutoRotateScreen
and get notified when screen orientation changes by registering toDeviceOrientationManager.OnScreenOrientationChanged
event.