LoginSignup
34

More than 5 years have passed since last update.

Unityからスマホ画面の向きの固定・解除(iOS・Android共通)

Last updated at Posted at 2016-01-12

Unity側にて処理が用意されているのでそれを使います。

Screen.orientation

・ScreenOrientation.Portrait
  縦画面(0度)

  • ScreenOrientation.PortraitUpsideDown
      上下反転の縦画面(180度回転)

  • ScreenOrientation.LandscapeLeft
      横画面(縦画面を0度とした時、左へ90度回転)

  • ScreenOrientation.LandscapeRight
      横画面(縦画面を0度とした時、右へ90度回転)

縦画面から横画面にして固定したい時

switch (Screen.orientation) {
    // 縦画面のとき
    case ScreenOrientation.Portrait:
        // 左回転して左向きの横画面にする
        Screen.orientation = ScreenOrientation.LandscapeLeft;
        break;
    // 上下反転の縦画面のとき
    case ScreenOrientation.PortraitUpsideDown:
        // 右回転して左向きの横画面にする
        Screen.orientation = ScreenOrientation.LandscapeRight;
        break;
}

固定を解除したい時

Screen.orientation = ScreenOrientation.AutoRotation;

AutoRotationをハンドリングしたい時

●AutoRotationとは?
 どの向きへの回転を許可するかの設定です。
●初期状態であればPlayerSettingsで設定可能です。
PlayerSettings->
   Resolution and Presentation->
      Allowed Orientations for Auto Rotation

■■スクリプトからいじる時は下記のような書き方で制御可能です。■■

●trueで回転を許可、falseで回転を許可しない。

// 縦
Screen.autorotateToPortrait = true;
// 左
Screen.autorotateToLandscapeLeft = true;
// 右
Screen.autorotateToLandscapeRight = true;
// 上下反転
Screen.autorotateToPortraitUpsideDown = true;

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34