以下の内容はhttps://redhologerbera.hatenablog.com/entry/2021/06/29/210513より取得しました。


UnityでXBoxコントローラーをつかう ボタンの取得

本日はUnity勉強枠です。

先日HoloLensでXBox コントローラーを接続しました。

今回はアプリを開発するうえでまずはUnity自体とXBoxコントローラーを接続します。

〇環境

 ・Windows 10PC

 ・Unity2021.1.10f1

 ・Xbox Wireless Controller

〇UnityでXBoxコントローラーをつかう

①WindowsPCとXBoxコントローラーをBluetooth接続します。

f:id:Holomoto-Sumire:20210629204356j:plain

②Unityでシーンを作成しデバッグ用のテキスト(TextMeshPro)を配置します。

f:id:Holomoto-Sumire:20210629204025j:plain

③次のスクリプトを作成し、からのゲームオブジェクトにアタッチします。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.InputSystem;
using Microsoft.MixedReality.Toolkit.Input;

public class GamePad : MonoBehaviour
{
    public TextMeshPro test;

    // Update is called once per frame
    void Update()
    {
        // ゲームパッドが接続されていないとnullになる。
        if (Gamepad.current == null) {
            test.text = "null";
        }
        if(Gamepad.current != null)
        {
            test.text = "OK";
        }
        if(Gamepad.current.buttonEast.isPressed)
        {
            test.text = "Button North Push";
        }
        if (Gamepad.current.buttonNorth.isPressed)
        {
            test.text="Button North Pushed!";
        }
        if (Gamepad.current.buttonSouth.isPressed)
        {
            test.text="Button South Rerease!";
        }
        if (Gamepad.current.buttonWest.isPressed)
        {
            test.text = "Button West Pushed";
        }
    }
}

④Unityを実行しコントローラーのボタンを入力します。

ボタンの入力を検知していることが確認できました。

f:id:Holomoto-Sumire:20210629205728g:plain

〇Unityでのゲームパッド

 以前のUnityでは[InputManager]を使用してコントローラーごとに提供されるキーを割り当てて設定する必要がありました。

 これはコントローラーごとに設定を行う必要があり、マルチプラットフォームという観点だけでなくターゲットが定まった開発でも非常に時間がかかるものでした。

 ここで[InputSystem]が登場しました。

 f:id:Holomoto-Sumire:20210629210239j:plain

この[InputSystem]を使用することで一般的なコントローラーのボタン配置などの共通点としてキー設定ができるようになりました。

docs.unity3d.com

今回のコードでは[Gamepad.current.buttonEast.isPressed]のように東西南北でボタン配置を設定しています。

今回は以上です。次回はボタン以外のコントローラーの入力を取得していきます。




以上の内容はhttps://redhologerbera.hatenablog.com/entry/2021/06/29/210513より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14