facebook twitter hatena line email

「Unity/Csharp/キー入力/InputSystem」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(InputSystem設定追加)
(InputActionのサンプル)
行74: 行74:
 
InputActionのとり方。
 
InputActionのとり方。
 
<pre>
 
<pre>
PlayerInput actionInput = GameObject.Find("PlayerInput").GetComponent<PlayerInput>();
+
using System.Collections;
InputActionAsset actions = actionInput.actions;
+
using System.Collections.Generic;
InputAction action = actionInput.actions["Fire"];
+
using UnityEngine;
 +
using UnityEngine.UI;
 +
using UniRx;
 +
using UnityEngine.InputSystem;
 +
public class InputSystemScene : MonoBehaviour
 +
{
 +
    InputAction fireAction;
 +
    void Awake()
 +
    {
 +
        PlayerInput actionInput = GameObject.Find("PlayerInput").GetComponent<PlayerInput>();
 +
        InputActionAsset actions = actionInput.actions;
 +
        fireAction = actionInput.actions["Fire"];
 +
    }
 +
    private void OnEnable()
 +
    {
 +
        fireAction.Enable();
 +
    }
 +
    private void OnDisable()
 +
    {
 +
        fireAction.Disable();
 +
    }
 +
    void Update()
 +
    {
 +
        if (fireAction.ReadValue<float>() > 0f)
 +
        {
 +
            Debug.Log("Fire");
 +
        }
 +
    }
 +
}
 
</pre>
 
</pre>
  

2021年11月9日 (火) 23:31時点における版

InputSytemとは

文字入力やマウスやゲームパッドなどのプラグイン

インストール

PackageManagerからRegistoryを選択して、"Input System"で検索して、Input Sytemをインストール。 今回は、ver1.0.2でした。

競合プラグイン

  • InputManager(旧
  • InputSystem(新

新旧があり、どちらかを指定することができる。

Unityメニュー/Edit/Project Setting/Player/Configuration

サンプル

using UnityEngine;
using UnityEngine.InputSystem;

public class SampleScene : MonoBehaviour
{
    void Update()
    {
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            Debug.Log("space key");
        }
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            Debug.Log("mouse down");
        }
        // キーボードのa
        if (Keyboard.current[Key.A].wasPressedThisFrame)
        {
            Debug.Log("A down");
        }
        /*
        if (Gamepad.current.buttonEast.isPressed)
        {
            Debug.Log("game pad o button");
        }
        */
    }
}

参考:https://gamedev65535.com/entry/unity_inputsystem_howtouse/

参考:https://gametukurikata.com/basic/inputsystem

InputSystem設定追加

  1. Assetsでcreate/Input Actionsを選択する
  2. Open input systemを選択し、保存すると、InputSystem.inputsettingができる
  3. OpenInput Settings Windowを開き、suppoted DevicesにKeyboardなどを登録

InputAction登録

  1. Assetsでcreate/Input Actionsを選択する
  2. NewControlsができるのでMyControlsへ変更
  3. Edit assetを選択
  4. 左上のNo Controll Schemeを選択
  5. add control schemeを選択
  6. +を選択してKeyBoardを選択
  7. ActionMapsはPlayerとする
  8. ActionsはFireとする
  9. Actionsの+を選択し、
  10. pathでSpace keyboardを選択する

InputActionのサンプル

(未完成)key取得はまだ。

  1. GameObjectを作成し、AddComponentからPlayInputを選択
  2. PlayerInputのActionsにMyControlsをドラッグ

InputActionのとり方。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using UnityEngine.InputSystem;
public class InputSystemScene : MonoBehaviour
{
    InputAction fireAction;
    void Awake()
    {
        PlayerInput actionInput = GameObject.Find("PlayerInput").GetComponent<PlayerInput>();
        InputActionAsset actions = actionInput.actions;
        fireAction = actionInput.actions["Fire"];
    }
    private void OnEnable()
    {
        fireAction.Enable();
    }
    private void OnDisable()
    {
        fireAction.Disable();
    }
    void Update()
    {
        if (fireAction.ReadValue<float>() > 0f)
        {
            Debug.Log("Fire");
        }
    }
}


参考:https://tsubakit1.hateblo.jp/entry/2019/01/09/001510

参考:https://gametukurikata.com/basic/inputsystem

参考:https://zenn.dev/k1togami/articles/eea2cd01d4199c