Skip to content

Instantly share code, notes, and snippets.

@Ethan-VisualVocal
Last active February 15, 2019 00:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ethan-VisualVocal/286ebae00eb3100885b19c90e8b9ff52 to your computer and use it in GitHub Desktop.
Save Ethan-VisualVocal/286ebae00eb3100885b19c90e8b9ff52 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!
}
}
}
@Ethan-VisualVocal
Copy link
Author

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.

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