タイトル: 「Set Input Mode」の種類と使い方
SEOタイトル: UE5 Set Input Mode の種類と使い分け完全ガイド
| この記事の要点 |
|
Set Input Mode とは
Unreal Engine の Set Input Mode は、プレイヤーの入力を「ゲーム」と「UI(Widget)」のどちらに渡すかを切り替える、Player Controller の関数群です。タイトル画面 / ゲームプレイ / ポーズメニュー / インベントリといった画面状態を作る上で必須の知識です。
3 種類の Set Input Mode
| モード | ゲーム入力 | UI 入力 | カーソル | 主な用途 |
|---|---|---|---|---|
| Set Input Mode Game Only | 受け取る | 受け取らない | 通常非表示 | ゲームプレイ中 |
| Set Input Mode UI Only | 受け取らない | 受け取る | 表示 | メインメニュー / ポーズ |
| Set Input Mode Game And UI | 受け取る | 受け取る | 表示可 | HUD・インベントリ・ミニマップ |
1. Set Input Mode Game Only
純粋なゲームプレイ中に使用。Widget があっても入力は届かず、すべて Player Controller / Pawn に流れます。
Blueprint での書き方
- Get Player Controller で PlayerController 取得
- Set Input Mode Game Only ノードを呼ぶ
- 合わせて Set Show Mouse Cursor → False
C++ での書き方
// MyPlayerController.cpp
#include "Blueprint/UserWidget.h"
void AMyPlayerController::EnterGameplay()
{
FInputModeGameOnly InputMode;
InputMode.SetConsumeCaptureMouseDown(true);
SetInputMode(InputMode);
bShowMouseCursor = false;
bEnableClickEvents = false;
bEnableMouseOverEvents = false;
}
2. Set Input Mode UI Only
メニュー画面・ポーズ画面・タイトル画面で使用。ゲーム入力は完全に止まり、UI 操作のみ可能になります。
必須パラメータ: In Widget to Focus
UI Only モードでは、キーボード入力を受け付ける Widget をフォーカス指定する必要があります。指定しないとキーボードナビゲーション(Tab、矢印キー)が効きません:
void AMyPlayerController::ShowPauseMenu()
{
PauseWidget = CreateWidget<UUserWidget>(this, PauseWidgetClass);
PauseWidget->AddToViewport();
FInputModeUIOnly InputMode;
InputMode.SetWidgetToFocus(PauseWidget->TakeWidget());
InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
SetInputMode(InputMode);
bShowMouseCursor = true;
UGameplayStatics::SetGamePaused(this, true);
}
Blueprint パターン
- Create Widget でメニュー Widget 生成
- Add to Viewport で表示
- Get Player Controller → Set Input Mode UI Only
- "In Widget to Focus" ピンに作成した Widget を接続
- Set Show Mouse Cursor → True
3. Set Input Mode Game And UI
もっとも汎用的。ゲームを動かしながら HUD・ミニマップ・チャット欄も操作したい場合に使用:
void AMyPlayerController::ShowInventory()
{
InventoryWidget = CreateWidget<UUserWidget>(this, InventoryClass);
InventoryWidget->AddToViewport();
FInputModeGameAndUI InputMode;
InputMode.SetWidgetToFocus(InventoryWidget->TakeWidget());
InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::LockOnCapture);
InputMode.SetHideCursorDuringCapture(false);
SetInputMode(InputMode);
bShowMouseCursor = true;
}
Lock Mouse To Viewport(カーソル拘束)
| EMouseLockMode | 意味 |
|---|---|
| DoNotLock | カーソルがウィンドウ外に出てもよい |
| LockOnCapture | マウスクリック時のみウィンドウ内に拘束 |
| LockAlways | 常時ウィンドウ内に拘束 |
| LockInFullscreen | フルスクリーン時のみ拘束 |
典型シナリオ別の書き分け
| 場面 | Set Input Mode | Show Cursor | 備考 |
|---|---|---|---|
| タイトル画面 | UI Only | True | BeginPlay で適用 |
| ゲーム開始 | Game Only | False | Possess 時 / レベル遷移時 |
| ポーズメニュー | UI Only | True | + SetGamePaused(true) |
| インベントリ | Game And UI | True | 背景でゲーム継続 |
| 会話ウィンドウ | UI Only | True | 移動を止める場面 |
| HUD のみ | Game Only | False | HUD は入力不要なので |
Show Mouse Cursor との関係
Set Input Mode は入力の振り分けを決めるだけで、カーソル表示は別フラグです:
// PlayerController のメンバ変数
bShowMouseCursor = true; // カーソル表示
bEnableClickEvents = true; // クリック判定
bEnableMouseOverEvents = true; // ホバー判定
Blueprint では Set Show Mouse Cursor ノード(Player Controller のメンバ関数)を使います。
BeginPlay で初期化
void AMyPlayerController::BeginPlay()
{
Super::BeginPlay();
// タイトル画面から始まる場合
if (TitleWidgetClass)
{
TitleWidget = CreateWidget<UUserWidget>(this, TitleWidgetClass);
TitleWidget->AddToViewport();
FInputModeUIOnly InputMode;
InputMode.SetWidgetToFocus(TitleWidget->TakeWidget());
SetInputMode(InputMode);
bShowMouseCursor = true;
}
}
よくあるトラブル
Q: UI Only にしたのに Tab / 矢印キーが効かない
A: Widget to Focus を指定していない。InputMode.SetWidgetToFocus(MyWidget->TakeWidget()) を必ず呼ぶ。
Q: Game And UI でゲーム入力が UI に吸われる
A: Widget の各ボタンの IsFocusable を確認。フォーカスが Widget にあるとキーが UI に流れる。クリック以外の入力を Game 側に通したいなら IsFocusable を Off。
Q: マウスが画面外に行ってしまう
A: SetLockMouseToViewportBehavior(EMouseLockMode::LockAlways) でウィンドウ内に拘束。
Enhanced Input との併用
UE5 標準の Enhanced Input でも Set Input Mode はそのまま有効。Input Mapping Context との組み合わせで「メニュー時はメニュー用 Context、ゲーム中はゲーム用 Context」と切り替えるとさらに整理できます:
void AMyPlayerController::ShowMenu()
{
if (auto Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
Subsystem->RemoveMappingContext(GameplayContext);
Subsystem->AddMappingContext(MenuContext, 0);
}
FInputModeUIOnly InputMode;
InputMode.SetWidgetToFocus(MenuWidget->TakeWidget());
SetInputMode(InputMode);
bShowMouseCursor = true;
}
FAQ
Q: SetInputMode はいつ呼ぶ?
A: 画面状態が切り替わる瞬間。タイトル → ゲーム開始時 / Esc で一時停止時 / インベントリ開閉時 など。状態管理を関数化(EnterGameplay / EnterMenu)すると保守しやすい。
Q: マルチプレイヤーで気をつけることは?
A: Set Input Mode はクライアントローカルの設定。リッスンサーバーホストでもクライアント側のコントローラ単位で呼ぶ。サーバー側で呼んでも反映されない。