「Xamarin.Androidで画面の向きを固定する」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
 
(同じ利用者による、間の5版が非表示)
4行目: 4行目:
 
縦方向に固定する場合。
 
縦方向に固定する場合。
 
<source lang="csharp">
 
<source lang="csharp">
    public class Activity1 : AndroidGameActivity
+
public class Activity1 : AndroidGameActivity
 +
{
 +
    protected override void OnCreate(Bundle bundle)
 
     {
 
     {
         protected override void OnCreate(Bundle bundle)
+
         base.OnCreate(bundle);
        {
 
            base.OnCreate(bundle);
 
  
            // 以下のRequestedOrientationプロパティに設定する方法だとダメ。
+
        // 以下のRequestedOrientationプロパティに設定する方法だとダメ。
            // 起動時に一瞬だけ横になりMonoGameの初期化ルーチンで画面サイズの情報がバグる。
+
        // 起動時に一瞬だけ横になりMonoGameの初期化ルーチンで画面サイズの情報がバグる。
            this.RequestedOrientation = ScreenOrientation.Portrait;
+
        //this.RequestedOrientation = ScreenOrientation.Portrait;
  
            var g = new GameMain();
+
        var g = new GameMain();
            SetContentView(g.Services.GetService<View>());
+
        SetContentView(g.Services.GetService<View>());
            g.Run();
+
        g.Run();
 +
    }
 +
   
 +
    // RequestedOrientationプロパティをoverrideする方法だと完璧に動くようだ。
 +
    public override ScreenOrientation RequestedOrientation
 +
    {
 +
        get
 +
        {
 +
            return ScreenOrientation.Portrait;
 
         }
 
         }
+
         set
         // RequestedOrientationプロパティをoverrideする方法だと完璧に動くようだ。
 
        public override ScreenOrientation RequestedOrientation
 
 
         {
 
         {
             get
+
             base.RequestedOrientation = ScreenOrientation.Portrait;
            {
 
                return ScreenOrientation.Portrait;
 
            }
 
            set
 
            {
 
                base.RequestedOrientation = ScreenOrientation.Portrait;
 
            }
 
 
         }
 
         }
 
     }
 
     }
 +
}
 
</source>
 
</source>
  
 
==関連項目==
 
==関連項目==
 
* [[MonoGame]]
 
* [[MonoGame]]
* [[MonoGame/BGMを再生する]]
+
* [[MonoGameでBGMを再生する]]
* [[Xamarin.Android/ディスプレイをスリープさせない]]
+
* [[Xamarin.Androidでディスプレイをスリープさせない]]
 
 
==参考文献==
 
{{reflist}}
 
 
 
{{stub}}
 
  
 
[[category:Xamarin.Android]]
 
[[category:Xamarin.Android]]
 
[[category:MonoGame]]
 
[[category:MonoGame]]

2021年4月20日 (火) 07:19時点における最新版

一般的なアプリでは画面は可変の方がいいのだろうが、MonoGameなどで作るゲームでは画面の向きを固定したいことが多い。

実装:ActivityのRequestedOrientationプロパティをoverrideする[編集 | ソースを編集]

縦方向に固定する場合。

public class Activity1 : AndroidGameActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // 以下のRequestedOrientationプロパティに設定する方法だとダメ。
        // 起動時に一瞬だけ横になりMonoGameの初期化ルーチンで画面サイズの情報がバグる。
        //this.RequestedOrientation = ScreenOrientation.Portrait;

        var g = new GameMain();
        SetContentView(g.Services.GetService<View>());
        g.Run();
    }
    
    // RequestedOrientationプロパティをoverrideする方法だと完璧に動くようだ。
    public override ScreenOrientation RequestedOrientation
    {
        get
        {
            return ScreenOrientation.Portrait;
        }
        set
        {
            base.RequestedOrientation = ScreenOrientation.Portrait;
        }
    }
}

関連項目[編集 | ソースを編集]