参考资料:
XBox键位说明(中/英文版):
https://support.xbox.com/zh-CN/xbox-one/accessories/xbox-one-wireless-controller
https://support.xbox.com/en-US/xbox-one/accessories/xbox-one-wireless-controller
Unity中的XBox控制器按键映射(英文版):
https://answers.unity.com/questions/1350081/xbox-one-controller-mapping-solved.html
版本说明:
本文内容在 Unity 2017.4.4f1 版本中进行了测试,附件内容也取自此版本的Unity项目。
Unity中XBox One控制器的按键映射关系如下,其中,注释中的 Axis
、Positive Button
和 字符串属性值
在Unity的 Input Manager 页面中会用到:
///
/// XBox One控制器按键映射信息类。
/// 轴属性用于 Input.GetAxis(string name) 方法;
/// 键属性用于 Input.GetKey(string name) 方法。
/// 所有属性不适用于 Input.GetButton(string name) 方法,如果要对按键使用此方法,
/// 应该传入此按键在Unity的“Input Manager”页面中的“Name”属性的值,而不是“Positive Button”属性的值。
///
public class XBoxController
{
#region 摇杆轴(Stick)
///
/// Left Stick Horizontal.
/// Axis: X Axis.
///
public string LSH { get { return "LSH"; } }
///
/// Left Stick Vertical.
/// Axis: Y Axis.
///
public string LSV { get { return "LSV"; } }
///
/// Right Stick Horizontal.
/// Axis: 4th Axis.
///
public string RSH { get { return "RSH"; } }
///
/// Right Stick Vertical.
/// Axis: 5th Axis.
///
public string RSV { get { return "RSV"; } }
#endregion
#region 十字方向键轴(Directional Pad,D-Pad)
///
/// D-Pad Horizontal.
/// Axis: 6th Axis.
///
public string DPadH { get { return "DPadH"; } }
///
/// D-Pad Vertical.
/// Axis: 7th Axis.
///
public string DPadV { get { return "DPadV"; } }
#endregion
#region 扳机轴(Trigger)
///
/// Left Trigger.
/// Axis: 9th Axis.
///
public string LT { get { return "LT"; } }
///
/// Right Trigger.
/// Axis: 10th Axis.
///
public string RT { get { return "RT"; } }
///
/// Shared Trigger.
/// Axis: 3rd Axis.
///
public string Trigger { get { return "Trigger"; } }
#endregion
#region ABXY键
///
/// A.
/// Positive Button: joystick button 0.
///
public string A { get { return "joystick button 0"; } }
///
/// B.
/// Positive Button: joystick button 1.
///
public string B { get { return "joystick button 1"; } }
///
/// X.
/// Positive Button: joystick button 2.
///
public string X { get { return "joystick button 2"; } }
///
/// Y.
/// Positive Button: joystick button 3.
///
public string Y { get { return "joystick button 3"; } }
#endregion
#region 缓冲键(Bumper)
///
/// Left Bumper.
/// Positive Button: joystick button 4.
///
public string LB { get { return "joystick button 4"; } }
///
/// Right Bumper.
/// Positive Button: joystick button 5.
///
public string RB { get { return "joystick button 5"; } }
#endregion
#region View(Back)、Menu(Start)、XBox键
///
/// View.
/// Positive Button: joystick button 6.
///
public string View { get { return "joystick button 6"; } }
///
/// Munu.
/// Positive Button: joystick button 7.
///
public string Menu { get { return "joystick button 7"; } }
// public string XBox { get { return string.None; } } // 没有对应
#endregion
#region 摇杆键
///
/// Left Stick Button.
/// Positive Button: joystick button 8.
///
public string LS { get { return "joystick button 8"; } } // JoystickButton8
///
/// Right Stick Button.
/// Positive Button: joystick button 9.
///
public string RS { get { return "joystick button 9"; } } // JoystickButton9
#endregion
}
在Unity中获取控制器按钮输入要使用 Input.GetKey(string name)
方法,获取控制器摇杆、方向键和扳机键输入要使用 Input.GetAxis(string axisName)
方法。
用于获取 按钮 输入的 GetKey()
方法的参数内容需要在 Input Manager 中通过 Positive Button
属性设置,Unity中每个按钮都有固定的名称,不能随意填写,参考上面的代码注释中的 Positive Button
内容进行设置,这个参数不受 Input Manager 中的 Name
属性影响。按钮的 Type
属性需要设置为 Key or Mouse Button 。
用于获取 轴 输入的 GetAxis()
方法的参数内容则需要在 Input Manager 中通过 Name
属性设置,这里可以随意设置名称,Unity中没有限定。轴的 Type
属性需要设置为 Joystick Axis ,Axis
属性参考上面的代码注释中的 Axis
内容进行设置,不能随意选择。
可以将下面的内容复制到项目文件夹下的 ProjectSettings/InputManager.asset 文件中生成对应上述代码和示意图的XBox输入配置。
注意这样做会覆盖原有的输入配置!
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!13 &1
InputManager:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Axes:
- serializedVersion: 3
m_Name: A
descriptiveName: XBox Controller Button "A".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 0
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: B
descriptiveName: XBox Controller Button "B".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 1
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: X
descriptiveName: "XBox Controller Button \u201CX\u201D."
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 2
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Y
descriptiveName: "XBox Controller Button \u201CY\u201D."
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 3
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: LB
descriptiveName: "XBox Controller Button \u201DLeft Bumper\u201C."
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 4
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: RB
descriptiveName: "XBox Controller Button \u201DRight Bumper\u201C."
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 5
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: View
descriptiveName: XBox Controller Button "View".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 6
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Menu
descriptiveName: XBox Controller Button "Menu".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 7
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: LS
descriptiveName: XBox Controller Button "Left Stick Button".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 8
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: RS
descriptiveName: XBox Controller Button "Right Stick Button".
descriptiveNegativeName:
negativeButton:
positiveButton: joystick button 9
altNegativeButton:
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: LSH
descriptiveName: XBox Controller Axis "Left Stick Horizontal".
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: LSV
descriptiveName: XBox Controller Axis "Left Stick Vertical".
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: RSH
descriptiveName: XBox Controller Axis "Right Stick Horizontal".
descriptiveNegativeName:
negativeButton:
positiveButton:
altNegativeButton:
altPositiveButton:
gravity: 0
dead: 0.19
sensitivity: 1
snap: 0
invert: 0
type: 2
axis: 3
joyNum: 0
- serializedVersion: 3
m_Name: RSV
descriptiveName: XBox Controller Axis "Right Stick Vertical".
descriptiveNegativeName:
negativeButton:
positiveButton:
altNegativeButton:
altPositiveButton:
gravity: 0
dead: 0.19
sensitivity: 1
snap: 0
invert: 1
type: 2
axis: 4
joyNum: 0
- serializedVersion: 3
m_Name: DPadH
descriptiveName: XBox Controller Axis "D-Pad Horizontal".
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: DPadV
descriptiveName: XBox Controller Axis "D-Pad Vertical".
descriptiveNegativeName:
negativeButton:
positiveButton:
altNegativeButton:
altPositiveButton:
gravity: 0
dead: 0.19
sensitivity: 1
snap: 0
invert: 0
type: 2
axis: 6
joyNum: 0
- serializedVersion: 3
m_Name: LT
descriptiveName: XBox Controller Axis "Left Trigger".
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: XBox Controller Axis "Right Trigger".
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: Trigger
descriptiveName: XBox Controller Axis "Shared Trigger".
descriptiveNegativeName:
negativeButton:
positiveButton:
altNegativeButton:
altPositiveButton:
gravity: 0
dead: 0.19
sensitivity: 1
snap: 0
invert: 0
type: 2
axis: 2
joyNum: 0
- serializedVersion: 3
m_Name: Horizontal
descriptiveName: Keyboard Axis "Horizontal".
descriptiveNegativeName:
negativeButton: left
positiveButton: right
altNegativeButton: a
altPositiveButton: d
gravity: 3
dead: 0.001
sensitivity: 3
snap: 1
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Vertical
descriptiveName: Keyboard Axis "Vertical".
descriptiveNegativeName:
negativeButton: down
positiveButton: up
altNegativeButton: s
altPositiveButton: w
gravity: 3
dead: 0.001
sensitivity: 3
snap: 1
invert: 0
type: 0
axis: 1
joyNum: 0
- serializedVersion: 3
m_Name: Horizontal
descriptiveName: XBox Controller Axis "Horizontal".
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: Vertical
descriptiveName: XBox Controller Axis "Vertical".
descriptiveNegativeName:
negativeButton:
positiveButton:
altNegativeButton:
altPositiveButton:
gravity: 0
dead: 0.19
sensitivity: 1
snap: 0
invert: 1
type: 2
axis: 1
joyNum: 0