入力
Turian には併用できる 2 つの層があります:
Input— デバイスに直接問い合わせます:W は押されている?InputActions— ゲームのアクションについて問い合わせます:ジャンプは押された? どのキーやボタンが各アクションを動かすかは Input Actions アセットにあり、デザイナーが編集し、プレイヤーが再割り当てできます。
どちらも Studio の Play Mode とエクスポートしたゲームで変更なしに動作します。キーコード(Key.W、MouseButton.Left)は Silk.NET のもので、新規プロジェクトでは Globals.cs でその名前空間をインポートしています。
デバイスへの問い合わせ
public class Mover : Component
{
public float speed = 4f;
public override void OnUpdate(float deltaTime)
{
var move = Vector3.Zero;
if (Input.IsKeyDown(Key.W)) move.Z += 1;
if (Input.IsKeyDown(Key.S)) move.Z -= 1;
if (Input.WasKeyPressed(Key.Space)) Log.Logger.LogInformation("ジャンプ!");
Node!.Transform.Position += move * speed * deltaTime;
}
}
| 問い合わせ | 戻り値 |
|---|---|
Input.IsKeyDown(key) / WasKeyPressed(key) / WasKeyReleased(key) |
押されている / このフレームで押された / このフレームで離された |
Input.IsMouseButtonDown(button)、WasMouseButtonPressed、WasMouseButtonReleased |
マウスボタンについて同様 |
Input.MousePosition、Input.MouseDelta、Input.MouseScrollDelta |
ポインターの位置、このフレームの移動量、ホイール |
Input.ConnectedGamepads |
接続されたゲームパッドのプレイヤー番号 |
Input.IsGamepadButtonDown(button, player)、WasGamepadButtonPressed、WasGamepadButtonReleased |
ゲームパッドのボタン。player が −1 ならどれでも |
Input.GetGamepadAxis(axis, player) |
スティックとトリガー、−1 から 1 |
ゲームパッドのボタンは位置による名前です — South、East、West、North(Xbox パッドの A/B/X/Y)、ショルダー、Back、Start、スティック押し込み、十字キー。軸は LeftStickX/Y、RightStickX/Y、LeftTrigger、RightTrigger です。
アクションマップ
1. アセットを作る
Assets パネルで右クリック → New → Input → Input Actions。選択すると Inspector で編集できます。
2. マップとアクションを定義する
アセットはマップ(例えば Gameplay と Menu)を持ち、各マップがアクションを持ちます:
| アクションの種類 | 読み取り方 | 例 |
|---|---|---|
Button |
IsPressed、WasPressed、WasReleased |
ジャンプ、射撃、ポーズ |
Axis |
ReadAxis()(−1 から 1) |
アクセル |
Vector2 |
ReadVector() |
移動、視点 |
各アクションはバインディングのリストを持ちます。バインディングはパスでコントロールを指定します — Keyboard/W、Mouse/Left、Mouse/DeltaX、Gamepad/South、Gamepad/LeftStickX — そして値を持ちます。ボタンなら押されたときに加わる値、軸なら読み取り値に掛ける値です。WASD の Move アクションには 4 つのバインディングがあります:W に (0, 1)、S に (0, −1)、A に (−1, 0)、D に (1, 0)。スティックのバインディングに (−1, 0) を指定すると反転します。
3. プロジェクトのアクションにする
プロジェクトの Input Settings(Assets/Settings/ 内)を開き、アセットを Actions にドロップします。ゲームは起動時にこれらのマップを読み込みます。
アクションの読み取り
public class PlayerController : Component
{
public float speed = 5f;
public override void OnUpdate(float deltaTime)
{
var move = InputActions.ReadVector("Gameplay/Move");
Node!.Transform.Position += new Vector3(move.X, 0, move.Y) * speed * deltaTime;
if (InputActions.WasPressed("Jump")) Jump();
}
void Jump() { /* … */ }
}
アクションは マップ/アクション で、またはすべてのマップを検索するなら名前だけで指定します。アクションアセットが設定されていない場合、問い合わせは例外を投げず中立の値を返します。
| 呼び出し | 効果 |
|---|---|
InputActions.IsPressed(path) / WasPressed / WasReleased |
ボタンの状態と変化 |
InputActions.ReadAxis(path) / ReadVector(path) |
アナログ値 |
InputActions.Find(path) |
Started、Performed、Canceled イベントを持つ InputAction |
InputActions.SetMapEnabled("Gameplay", false) |
マップ全体を止める — 例えばメニューを開いている間 |
実行時の再割り当て
InputActionService を使うと、設定メニューで操作を再割り当てできます:
var service = InputActions.Service!;
var jump = service.Find("Gameplay/Jump")!;
var rebind = service.StartRebind(jump, bindingIndex: 0); // 次のキーまたはボタンを待つ
// … 毎フレーム rebind.IsComplete / rebind.WasCancelled を確認。service.CancelRebind() で中止。
エクスポートしたゲームは起動時に、ユーザーのアプリケーションデータフォルダー内の、ゲームの製品名のフォルダーにある inputbindings.json からプレイヤーの再割り当てを復元します。InputBindingStore.Save(service, InputBindingStore.DefaultPath(product)) で保存します。
Studio では
Play Mode は Game ビューにフォーカスがある間、キーボードとマウスをゲームに渡します。中をクリックして遊び、Esc でキーボードを Studio に戻します。