Configure screen orientation settings in Swift projects.
The Swift Xcode project type strictly enforces the orientation settings you configure in Player SettingsSettings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary. This behavior is different from the Objective-C project type, which allows for more flexibility at runtime.
When you build your project, iOS uses the Allowed Orientations for Auto Rotation settings in Player Settings to determine which orientations the application supports.
The key difference between the project types is how they handle these settings at runtime:
For example, if you only enable Landscape Left and Landscape Right in Player Settings:
Follow these steps to replicate the screen orientation behavior of an Objective-C project in a Swift project:
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToLandscapeLeft = true;
You can then update the flags when you need to adjust the screen’s orientation.
For example, the following code changes autorotation orientations to portrait only:
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToPortrait = true;
Screen.autorotateToPortraitUpsideDown = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToLandscapeLeft = false;
Alternatively, you can force the screen into a single, specific orientation.
Screen.orientation = ScreenOrientation.Portrait;