Skip to content

Instantly share code, notes, and snippets.

@Enigo
Last active February 23, 2021 22:39
Show Gist options
  • Save Enigo/1797e65f74cf12e8533a2a94b7bef601 to your computer and use it in GitHub Desktop.
Save Enigo/1797e65f74cf12e8533a2a94b7bef601 to your computer and use it in GitHub Desktop.
public class OrientationSetter : MonoBehaviour
{
private static IEnumerator SetupInitialOrientation()
{
if (PlayerPrefsUtils.HasOrientation())
{
if (PlayerPrefsUtils.IsPortrait())
{
SetupPortraitOrientation();
if (Screen.orientation != ScreenOrientation.Portrait)
{
Screen.orientation = ScreenOrientation.Portrait;
yield return new WaitForSeconds(3);
LoadScene();
}
}
else
{
SetupLandscapeOrientation();
if (Screen.orientation != ScreenOrientation.Landscape)
{
Screen.orientation = ScreenOrientation.Landscape;
yield return new WaitForSeconds(3);
LoadScene();
}
}
yield break;
}
if (Screen.orientation == ScreenOrientation.Portrait)
{
SetupPortraitOrientation();
}
else
{
SetupLandscapeOrientation();
}
LoadScene();
}
private static void SetupPortraitOrientation()
{
Screen.autorotateToPortrait = true;
Screen.autorotateToPortraitUpsideDown = true;
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
}
private static void SetupLandscapeOrientation()
{
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment