Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Created February 8, 2018 22:14
Show Gist options
  • Save yasirkula/e056df5e7af1ce2de98c6372d0f26ade to your computer and use it in GitHub Desktop.
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.
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;
}
}
}
@yasirkula
Copy link
Author

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

  1. In Player Settings, set Allowed Orientations for Auto Rotation as you wish
  2. Change the value of Default Orientation to something other than Auto Rotation
  3. That's it! Now you can toggle auto screen orientation via DeviceOrientationManager.AutoRotateScreen and get notified when screen orientation changes by registering to DeviceOrientationManager.OnScreenOrientationChanged event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment