Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Forked from Ethan-VisualVocal/ForceLandscape.cs
Created October 17, 2017 04:35
Show Gist options
  • Save vinhnx/539d821cb174c338028843000b5f80ba to your computer and use it in GitHub Desktop.
Save vinhnx/539d821cb174c338028843000b5f80ba to your computer and use it in GitHub Desktop.
Workaround for Unity set Screen.orientation bug on iOS 10.
using UnityEngine;
using System.Collections;
// This is used to workaround https://issuetracker.unity3d.com/issues/ios-changing-the-screen-orientation-via-a-script-sometimes-results-in-corrupted-view-on-ios-10
// Bug shows screen in portrait with content rotated 90 offscreen. Caused by explicitly setting Landscape orientation on iOS 10.
//
// On iOS this just switches to LandscapeLeft, back to Portrait, and then back to LandscapeLeft, which seems to work.
// SUGGESTION: blank out the screen before executing this, since the screen jumps around as it switches back and forth.
public class ForceLandscape : MonoBehaviour
{
public void ForceLandscapeLeft()
{
#if UNITY_IOS && !UNITY_EDITOR
StartCoroutine(ForceAndFixLandscape());
#else
Screen.orientation = ScreenOrientation.LandscapeLeft;
#endif
}
IEnumerator ForceAndFixLandscape()
{
ScreenOrientation prev = ScreenOrientation.Portrait;
for (int i = 0; i < 3; i++)
{
Screen.orientation =
(prev == ScreenOrientation.Portrait ? ScreenOrientation.LandscapeLeft : ScreenOrientation.Portrait);
yield return new WaitWhile(() => {
return prev == Screen.orientation;
});
prev = Screen.orientation;
yield return new WaitForSeconds(0.5f); //this is an arbitrary wait value -- it may need tweaking for different iPhones!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment