在Unity中使用XBox手柄进行输入的控制管理,首先需要在Project Settings / Input Manager中进行按键的配置,下图是Unity默认的Input Manager配置:
XBox的按键在Unity中的对应关系如下:
usingUnityEngine; namespaceSK.Framework{ /// <summary>/// XBox按键/// </summary>publicclassXBox { /// <summary>/// 左侧摇杆水平轴/// X axis/// </summary>publicconststringLeftStickHorizontal="LeftStickHorizontal"; /// <summary>/// 左侧摇杆垂直轴/// Y axis/// </summary>publicconststringLeftStickVertical="LeftStickVertical"; /// <summary>/// 右侧摇杆水平轴/// 4th axis/// </summary>publicconststringRightStickHorizontal="RightStickHorizontal"; /// <summary>/// 右侧摇杆垂直轴/// 5th axis/// </summary>publicconststringRightStickVertical="RightStickVertical"; /// <summary>/// 十字方向盘水平轴/// 6th axis/// </summary>publicconststringDPadHorizontal="DPadHorizontal"; /// <summary>/// 十字方向盘垂直轴/// 7th axis/// </summary>publicconststringDPadVertical="DPadVertical"; /// <summary>/// LT/// 9th axis/// </summary>publicconststringLT="LT"; /// <summary>/// RT/// 10th axis/// </summary>publicconststringRT="RT"; /// <summary>/// 左侧摇杆按键/// joystick button 8/// </summary>publicconstKeyCodeLeftStick=KeyCode.JoystickButton8; /// <summary>/// 右侧摇杆按键/// joystick button 9/// </summary>publicconstKeyCodeRightStick=KeyCode.JoystickButton9; /// <summary>/// A键/// joystick button 0/// </summary>publicconstKeyCodeA=KeyCode.JoystickButton0; /// <summary>/// B键/// joystick button 1/// </summary>publicconstKeyCodeB=KeyCode.JoystickButton1; /// <summary>/// X键/// joystick button 2/// </summary>publicconstKeyCodeX=KeyCode.JoystickButton2; /// <summary>/// Y键/// joystick button 3/// </summary>publicconstKeyCodeY=KeyCode.JoystickButton3; /// <summary>/// LB键/// joystick button 4/// </summary>publicconstKeyCodeLB=KeyCode.JoystickButton4; /// <summary>/// RB键/// joystick button 5/// </summary>publicconstKeyCodeRB=KeyCode.JoystickButton5; /// <summary>/// View视图键/// joystick button 6/// </summary>publicconstKeyCodeView=KeyCode.JoystickButton6; /// <summary>/// Menu菜单键/// joystick button 7/// </summary>publicconstKeyCodeMenu=KeyCode.JoystickButton7; } }
根据上面的对应关系,在Input Manager中间配置,例如Left Stick Horizontal,即手柄左侧摇杆的水平轴,对应的Axis为X axis,那么它在Input Manager中的配置如下:
右侧摇杆的垂直轴对应的Axis为5th axis,那么它在Input Manager中的配置如下:
同理进行其他按键的配置:
可直接编辑项目根目录下ProjectSettings文件夹中的InputManager.asset内容,使用以下内容覆盖:
%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的按键说明:
测试脚本:
usingUnityEngine; usingSK.Framework; publicclassExample : MonoBehaviour{ privatevoidUpdate() { 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"); floatlh=Input.GetAxis(XBox.LeftStickHorizontal); floatlv=Input.GetAxis(XBox.LeftStickVertical); floatrh=Input.GetAxis(XBox.RightStickHorizontal); floatrv=Input.GetAxis(XBox.RightStickVertical); floatlt=Input.GetAxis(XBox.LT); floatrt=Input.GetAxis(XBox.RT); floatdPadH=Input.GetAxis(XBox.DPadHorizontal); floatdPadV=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}"); } }