Unity【XBox One】- Configuration and use of controller input

To use the XBox handle for input control management in Unity, you first need to configure the keys in Project Settings / Input Manager. The following figure shows the default Input Manager configuration of Unity:

The corresponding relationship of XBox buttons in Unity is as follows:

using UnityEngine;

namespace SK.Framework
{
    /// <summary>
    /// XBox按键
    /// </summary>
    public class XBox
    {
        /// <summary>
        /// 左侧摇杆水平轴
        /// X axis
        /// </summary>
        public const string LeftStickHorizontal = "LeftStickHorizontal";
        /// <summary>
        /// 左侧摇杆垂直轴
        /// Y axis
        /// </summary>
        public const string LeftStickVertical = "LeftStickVertical";
        /// <summary>
        /// 右侧摇杆水平轴
        /// 4th axis
        /// </summary>
        public const string RightStickHorizontal = "RightStickHorizontal";
        /// <summary>
        /// 右侧摇杆垂直轴
        /// 5th axis
        /// </summary>
        public const string RightStickVertical = "RightStickVertical";
        /// <summary>
        /// 十字方向盘水平轴
        /// 6th axis
        /// </summary>
        public const string DPadHorizontal = "DPadHorizontal";
        /// <summary>
        /// 十字方向盘垂直轴
        /// 7th axis
        /// </summary>
        public const string DPadVertical = "DPadVertical";
        /// <summary>
        /// LT
        /// 9th axis
        /// </summary>
        public const string LT = "LT";
        /// <summary>
        /// RT
        /// 10th axis
        /// </summary>
        public const string RT = "RT";
        /// <summary>
        /// 左侧摇杆按键
        /// joystick button 8
        /// </summary>
        public const KeyCode LeftStick = KeyCode.JoystickButton8;
        /// <summary>
        /// 右侧摇杆按键
        /// joystick button 9
        /// </summary>
        public const KeyCode RightStick = KeyCode.JoystickButton9;
        /// <summary>
        /// A键
        /// joystick button 0
        /// </summary>
        public const KeyCode A = KeyCode.JoystickButton0;
        /// <summary>
        /// B键
        /// joystick button 1
        /// </summary>
        public const KeyCode B = KeyCode.JoystickButton1;
        /// <summary>
        /// X键
        /// joystick button 2
        /// </summary>
        public const KeyCode X = KeyCode.JoystickButton2;
        /// <summary>
        /// Y键
        /// joystick button 3
        /// </summary>
        public const KeyCode Y = KeyCode.JoystickButton3;
        /// <summary>
        /// LB键
        /// joystick button 4
        /// </summary>
        public const KeyCode LB = KeyCode.JoystickButton4;
        /// <summary>
        /// RB键
        /// joystick button 5
        /// </summary>
        public const KeyCode RB = KeyCode.JoystickButton5;
        /// <summary>
        /// View视图键
        /// joystick button 6
        /// </summary>
        public const KeyCode View = KeyCode.JoystickButton6;
        /// <summary>
        /// Menu菜单键
        /// joystick button 7
        /// </summary>
        public const KeyCode Menu = KeyCode.JoystickButton7;
    }
}

According to the above correspondence, configure it in the middle of the Input Manager, such as Left Stick Horizontal, which is the horizontal axis of the joystick on the left side of the handle, and the corresponding Axis is the X axis, then its configuration in the Input Manager is as follows:

The Axis corresponding to the vertical axis of the right joystick is the 5th axis, then its configuration in the Input Manager is as follows:

In the same way, configure other buttons:

You can directly edit the InputManager.asset content in the ProjectSettings folder in the project root directory and overwrite it with the following content:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!13 &1
InputManager:
  m_ObjectHideFlags: 0
  serializedVersion: 2
  m_Axes:
  - serializedVersion: 3
    m_Name: LeftStickHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LeftStickVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 1
    joyNum: 0
  - serializedVersion: 3
    m_Name: A
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: B
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 1
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: X
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 2
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: Y
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 3
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LB
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 4
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: RB
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 5
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LeftStick
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 8
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStick
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 9
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: View
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 6
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: Menu
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 7
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LT
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 8
    joyNum: 0
  - serializedVersion: 3
    m_Name: RT
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 9
    joyNum: 0
  - serializedVersion: 3
    m_Name: DPadHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 5
    joyNum: 0
  - serializedVersion: 3
    m_Name: DPadVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 6
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStickHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 3
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStickVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 4
    joyNum: 0

Attach the button description of XBox:

Test script:

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(XBox.A)) Debug.Log("A"); 
        if (Input.GetKeyDown(XBox.B)) Debug.Log("B");
        if (Input.GetKeyDown(XBox.X)) Debug.Log("X");
        if (Input.GetKeyDown(XBox.Y)) Debug.Log("Y");
        if (Input.GetKeyDown(XBox.LB)) Debug.Log("LB");
        if (Input.GetKeyDown(XBox.RB)) Debug.Log("RB");
        if (Input.GetKeyDown(XBox.View)) Debug.Log("View");
        if (Input.GetKeyDown(XBox.Menu)) Debug.Log("Menu");

        float lh = Input.GetAxis(XBox.LeftStickHorizontal);
        float lv = Input.GetAxis(XBox.LeftStickVertical);
        float rh = Input.GetAxis(XBox.RightStickHorizontal);
        float rv = Input.GetAxis(XBox.RightStickVertical);
        float lt = Input.GetAxis(XBox.LT);
        float rt = Input.GetAxis(XBox.RT);
        float dPadH = Input.GetAxis(XBox.DPadHorizontal);
        float dPadV = Input.GetAxis(XBox.DPadVertical);

        if (lh != 0) Debug.Log($"LH:{lh}");
        if (lv != 0) Debug.Log($"LV:{lv}");
        if (rh != 0) Debug.Log($"RH:{rh}");
        if (rv != 0) Debug.Log($"RV:{rv}");
        if (lt != 0) Debug.Log($"LT:{lt}");
        if (rt != 0) Debug.Log($"RT:{rt}");
        if (dPadH != 0) Debug.Log($"DPadH:{dPadH}");
        if (dPadV != 0) Debug.Log($"DPadV:{dPadV}");
    }
}

  Welcome to the public account "Contemporary Wild Programmer"

おすすめ

転載: blog.csdn.net/qq_42139931/article/details/123256358