「Xamarin.Androidで画面の向きを固定する」の版間の差分
ナビゲーションに移動
検索に移動
Administrator (トーク | 投稿記録) (→関連項目) |
Administrator (トーク | 投稿記録) |
||
4行目: | 4行目: | ||
縦方向に固定する場合。 | 縦方向に固定する場合。 | ||
<source lang="csharp"> | <source lang="csharp"> | ||
− | + | 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; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
+ | } | ||
</source> | </source> | ||
2021年4月20日 (火) 07:18時点における版
一般的なアプリでは画面は可変の方がいいのだろうが、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;
}
}
}