Unity【XBox One】- 手柄输入的配置与使用

2022-08-29 16:41:26 浏览数 (1)

在Unity中使用XBox手柄进行输入的控制管理,首先需要在Project Settings / Input Manager中进行按键的配置,下图是Unity默认的Input Manager配置:

XBox的按键在Unity中的对应关系如下:

代码语言:javascript复制
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;
    }
}

根据上面的对应关系,在Input Manager中间配置,例如Left Stick Horizontal,即手柄左侧摇杆的水平轴,对应的Axis为X axis,那么它在Input Manager中的配置如下:

右侧摇杆的垂直轴对应的Axis为5th axis,那么它在Input Manager中的配置如下:

同理进行其他按键的配置:

可直接编辑项目根目录下ProjectSettings文件夹中的InputManager.asset内容,使用以下内容覆盖:

代码语言:javascript复制
%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

附上XBox的按键说明:

测试脚本:

代码语言:javascript复制
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}");
    }
}

0 人点赞