Manage multiple gamepads

Page update date :
Page creation date :

Verification environment

Windows
  • Windows 11
Unity Editor
  • 2020.3.25f1
Input System Package
  • 1.2.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

At first

To get Gamepad.all information about multiple gamepads can be achieved by obtaining . ReadOnlyArray Since it foreach is defined in .

In this tip, I would like to display the information of each gamepad in text. Gamepad Since the class is used, when running on Windows, only controllers that can use "Xinput" are eligible.

Also, please refer to the following page for the basic handling of gamepads.

Get information about multiple gamepads

Arrange a text object to display gamepad information. This time, you can display up to 4 gamepads.

Create a script. The name is arbitrary, but for now GamepadAll we will leave it as .

The script looks like this: This time, the purpose is to list gamepads, so see "Operating with a gamepad (input system package version)" for information on obtaining each information.

using System.Text;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class GamepadAll : MonoBehaviour
{
  /// <summary>情報を表示させるテキストオブジェクト一覧。</summary>
  [SerializeField] private Text[] TextObjects;

  StringBuilder Builder = new StringBuilder();
  StringBuilder BuilderButton = new StringBuilder();

  // 更新はフレームごとに1回呼び出されます
  void Update()
  {
    if (TextObjects == null)
    {
      Debug.Log($"{nameof(TextObjects)} が null です。");
      return;
    }

    // Gamepad.all で接続されているすべてのゲームパッドを列挙できる
    // TextObjects の数以上の情報は載せられないので、少ない方の数で for する
    for (int i = 0; i < Gamepad.all.Count || i < TextObjects.Length; i++)
		{
      var gamepad = Gamepad.all[i];
      var textObject = TextObjects[i];

      Builder.Clear();
      BuilderButton.Clear();

      Builder.AppendLine($"deviceId:{gamepad.deviceId}");
      Builder.AppendLine($"name:{gamepad.name}");

      // 操作されたボタンなどの情報を取得
      var leftStickValue = gamepad.leftStick.ReadValue();
      var rightStickValue = gamepad.rightStick.ReadValue();
      var dpadValue = gamepad.dpad.ReadValue();

      if (leftStickValue.magnitude > 0f) Builder.AppendLine($"LeftStick:{leftStickValue.normalized * leftStickValue.magnitude}");
      if (rightStickValue.magnitude > 0f) Builder.AppendLine($"RightStick:{rightStickValue.normalized * rightStickValue.magnitude}");
      if (dpadValue.magnitude > 0f) Builder.AppendLine($"Dpad:{dpadValue.normalized * dpadValue.magnitude}");

      if (gamepad.aButton.isPressed) BuilderButton.Append($"A ");
      if (gamepad.bButton.isPressed) BuilderButton.Append($"B ");
      if (gamepad.xButton.isPressed) BuilderButton.Append($"X ");
      if (gamepad.yButton.isPressed) BuilderButton.Append($"Y ");

      if (gamepad.startButton.isPressed) BuilderButton.Append($"Start ");
      if (gamepad.selectButton.isPressed) BuilderButton.Append($"Select ");

      if (gamepad.leftStickButton.isPressed) BuilderButton.Append($"LeftStickButton ");
      if (gamepad.rightStickButton.isPressed) BuilderButton.Append($"RightStickButton ");

      if (gamepad.leftShoulder.isPressed) BuilderButton.Append($"LeftShoulder ");
      if (gamepad.rightShoulder.isPressed) BuilderButton.Append($"RightShoulder ");

      if (BuilderButton.Length >= 1) Builder.AppendLine(BuilderButton.ToString());

      var leftTriggerValue = gamepad.leftTrigger.ReadValue();
      var rightTriggerValue = gamepad.rightTrigger.ReadValue();

      if (leftTriggerValue > 0 || rightTriggerValue > 0)
      {
        Builder.AppendLine($"Trigger:({leftTriggerValue:f2}, {rightTriggerValue:f2})");
      }

      // 取得した情報を表示
      textObject.text = Builder.ToString();
    }
  }
}

Gamepad.all You can enumerate all connected gamepads in . ReadOnlyArray Since it for foreach is defined in , it can be enumerated by .

After EventSystem you save the script, attach it to . TextObjects is defined as an array, so you can set multiple text objects.

Try running the game and controlling the gamepad. If you have multiple gamepads connected, you will see as many information as you want.