当前位置:   article > 正文

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

joystick button

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

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

  1. using UnityEngine;
  2. namespace SK.Framework
  3. {
  4. /// <summary>
  5. /// XBox按键
  6. /// </summary>
  7. public class XBox
  8. {
  9. /// <summary>
  10. /// 左侧摇杆水平轴
  11. /// X axis
  12. /// </summary>
  13. public const string LeftStickHorizontal = "LeftStickHorizontal";
  14. /// <summary>
  15. /// 左侧摇杆垂直轴
  16. /// Y axis
  17. /// </summary>
  18. public const string LeftStickVertical = "LeftStickVertical";
  19. /// <summary>
  20. /// 右侧摇杆水平轴
  21. /// 4th axis
  22. /// </summary>
  23. public const string RightStickHorizontal = "RightStickHorizontal";
  24. /// <summary>
  25. /// 右侧摇杆垂直轴
  26. /// 5th axis
  27. /// </summary>
  28. public const string RightStickVertical = "RightStickVertical";
  29. /// <summary>
  30. /// 十字方向盘水平轴
  31. /// 6th axis
  32. /// </summary>
  33. public const string DPadHorizontal = "DPadHorizontal";
  34. /// <summary>
  35. /// 十字方向盘垂直轴
  36. /// 7th axis
  37. /// </summary>
  38. public const string DPadVertical = "DPadVertical";
  39. /// <summary>
  40. /// LT
  41. /// 9th axis
  42. /// </summary>
  43. public const string LT = "LT";
  44. /// <summary>
  45. /// RT
  46. /// 10th axis
  47. /// </summary>
  48. public const string RT = "RT";
  49. /// <summary>
  50. /// 左侧摇杆按键
  51. /// joystick button 8
  52. /// </summary>
  53. public const KeyCode LeftStick = KeyCode.JoystickButton8;
  54. /// <summary>
  55. /// 右侧摇杆按键
  56. /// joystick button 9
  57. /// </summary>
  58. public const KeyCode RightStick = KeyCode.JoystickButton9;
  59. /// <summary>
  60. /// A键
  61. /// joystick button 0
  62. /// </summary>
  63. public const KeyCode A = KeyCode.JoystickButton0;
  64. /// <summary>
  65. /// B键
  66. /// joystick button 1
  67. /// </summary>
  68. public const KeyCode B = KeyCode.JoystickButton1;
  69. /// <summary>
  70. /// X键
  71. /// joystick button 2
  72. /// </summary>
  73. public const KeyCode X = KeyCode.JoystickButton2;
  74. /// <summary>
  75. /// Y键
  76. /// joystick button 3
  77. /// </summary>
  78. public const KeyCode Y = KeyCode.JoystickButton3;
  79. /// <summary>
  80. /// LB键
  81. /// joystick button 4
  82. /// </summary>
  83. public const KeyCode LB = KeyCode.JoystickButton4;
  84. /// <summary>
  85. /// RB键
  86. /// joystick button 5
  87. /// </summary>
  88. public const KeyCode RB = KeyCode.JoystickButton5;
  89. /// <summary>
  90. /// View视图键
  91. /// joystick button 6
  92. /// </summary>
  93. public const KeyCode View = KeyCode.JoystickButton6;
  94. /// <summary>
  95. /// Menu菜单键
  96. /// joystick button 7
  97. /// </summary>
  98. public const KeyCode Menu = KeyCode.JoystickButton7;
  99. }
  100. }

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

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

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

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

  1. %YAML 1.1
  2. %TAG !u! tag:unity3d.com,2011:
  3. --- !u!13 &1
  4. InputManager:
  5. m_ObjectHideFlags: 0
  6. serializedVersion: 2
  7. m_Axes:
  8. - serializedVersion: 3
  9. m_Name: LeftStickHorizontal
  10. descriptiveName:
  11. descriptiveNegativeName:
  12. negativeButton:
  13. positiveButton:
  14. altNegativeButton:
  15. altPositiveButton:
  16. gravity: 0
  17. dead: 0.19
  18. sensitivity: 1
  19. snap: 0
  20. invert: 0
  21. type: 2
  22. axis: 0
  23. joyNum: 0
  24. - serializedVersion: 3
  25. m_Name: LeftStickVertical
  26. descriptiveName:
  27. descriptiveNegativeName:
  28. negativeButton:
  29. positiveButton:
  30. altNegativeButton:
  31. altPositiveButton:
  32. gravity: 0
  33. dead: 0.19
  34. sensitivity: 1
  35. snap: 0
  36. invert: 1
  37. type: 2
  38. axis: 1
  39. joyNum: 0
  40. - serializedVersion: 3
  41. m_Name: A
  42. descriptiveName:
  43. descriptiveNegativeName:
  44. negativeButton:
  45. positiveButton:
  46. altNegativeButton:
  47. altPositiveButton:
  48. gravity: 0
  49. dead: 0.001
  50. sensitivity: 1
  51. snap: 0
  52. invert: 0
  53. type: 0
  54. axis: 0
  55. joyNum: 0
  56. - serializedVersion: 3
  57. m_Name: B
  58. descriptiveName:
  59. descriptiveNegativeName:
  60. negativeButton:
  61. positiveButton: joystick button 1
  62. altNegativeButton:
  63. altPositiveButton:
  64. gravity: 0
  65. dead: 0.001
  66. sensitivity: 1
  67. snap: 0
  68. invert: 0
  69. type: 0
  70. axis: 0
  71. joyNum: 0
  72. - serializedVersion: 3
  73. m_Name: X
  74. descriptiveName:
  75. descriptiveNegativeName:
  76. negativeButton:
  77. positiveButton: joystick button 2
  78. altNegativeButton:
  79. altPositiveButton:
  80. gravity: 0
  81. dead: 0.001
  82. sensitivity: 1
  83. snap: 0
  84. invert: 0
  85. type: 0
  86. axis: 0
  87. joyNum: 0
  88. - serializedVersion: 3
  89. m_Name: Y
  90. descriptiveName:
  91. descriptiveNegativeName:
  92. negativeButton:
  93. positiveButton: joystick button 3
  94. altNegativeButton:
  95. altPositiveButton:
  96. gravity: 0
  97. dead: 0.001
  98. sensitivity: 1
  99. snap: 0
  100. invert: 0
  101. type: 0
  102. axis: 0
  103. joyNum: 0
  104. - serializedVersion: 3
  105. m_Name: LB
  106. descriptiveName:
  107. descriptiveNegativeName:
  108. negativeButton:
  109. positiveButton: joystick button 4
  110. altNegativeButton:
  111. altPositiveButton:
  112. gravity: 0
  113. dead: 0.001
  114. sensitivity: 1
  115. snap: 0
  116. invert: 0
  117. type: 0
  118. axis: 0
  119. joyNum: 0
  120. - serializedVersion: 3
  121. m_Name: RB
  122. descriptiveName:
  123. descriptiveNegativeName:
  124. negativeButton:
  125. positiveButton: joystick button 5
  126. altNegativeButton:
  127. altPositiveButton:
  128. gravity: 0
  129. dead: 0.001
  130. sensitivity: 1
  131. snap: 0
  132. invert: 0
  133. type: 0
  134. axis: 0
  135. joyNum: 0
  136. - serializedVersion: 3
  137. m_Name: LeftStick
  138. descriptiveName:
  139. descriptiveNegativeName:
  140. negativeButton:
  141. positiveButton: joystick button 8
  142. altNegativeButton:
  143. altPositiveButton:
  144. gravity: 0
  145. dead: 0.001
  146. sensitivity: 1
  147. snap: 0
  148. invert: 0
  149. type: 0
  150. axis: 0
  151. joyNum: 0
  152. - serializedVersion: 3
  153. m_Name: RightStick
  154. descriptiveName:
  155. descriptiveNegativeName:
  156. negativeButton:
  157. positiveButton: joystick button 9
  158. altNegativeButton:
  159. altPositiveButton:
  160. gravity: 0
  161. dead: 0.001
  162. sensitivity: 1
  163. snap: 0
  164. invert: 0
  165. type: 0
  166. axis: 0
  167. joyNum: 0
  168. - serializedVersion: 3
  169. m_Name: View
  170. descriptiveName:
  171. descriptiveNegativeName:
  172. negativeButton:
  173. positiveButton: joystick button 6
  174. altNegativeButton:
  175. altPositiveButton:
  176. gravity: 0
  177. dead: 0.001
  178. sensitivity: 1
  179. snap: 0
  180. invert: 0
  181. type: 0
  182. axis: 0
  183. joyNum: 0
  184. - serializedVersion: 3
  185. m_Name: Menu
  186. descriptiveName:
  187. descriptiveNegativeName:
  188. negativeButton:
  189. positiveButton: joystick button 7
  190. altNegativeButton:
  191. altPositiveButton:
  192. gravity: 0
  193. dead: 0.001
  194. sensitivity: 1
  195. snap: 0
  196. invert: 0
  197. type: 0
  198. axis: 0
  199. joyNum: 0
  200. - serializedVersion: 3
  201. m_Name: LT
  202. descriptiveName:
  203. descriptiveNegativeName:
  204. negativeButton:
  205. positiveButton:
  206. altNegativeButton:
  207. altPositiveButton:
  208. gravity: 0
  209. dead: 0.19
  210. sensitivity: 1
  211. snap: 0
  212. invert: 0
  213. type: 2
  214. axis: 8
  215. joyNum: 0
  216. - serializedVersion: 3
  217. m_Name: RT
  218. descriptiveName:
  219. descriptiveNegativeName:
  220. negativeButton:
  221. positiveButton:
  222. altNegativeButton:
  223. altPositiveButton:
  224. gravity: 0
  225. dead: 0.19
  226. sensitivity: 1
  227. snap: 0
  228. invert: 0
  229. type: 2
  230. axis: 9
  231. joyNum: 0
  232. - serializedVersion: 3
  233. m_Name: DPadHorizontal
  234. descriptiveName:
  235. descriptiveNegativeName:
  236. negativeButton:
  237. positiveButton:
  238. altNegativeButton:
  239. altPositiveButton:
  240. gravity: 0
  241. dead: 0.19
  242. sensitivity: 1
  243. snap: 0
  244. invert: 0
  245. type: 2
  246. axis: 5
  247. joyNum: 0
  248. - serializedVersion: 3
  249. m_Name: DPadVertical
  250. descriptiveName:
  251. descriptiveNegativeName:
  252. negativeButton:
  253. positiveButton:
  254. altNegativeButton:
  255. altPositiveButton:
  256. gravity: 0
  257. dead: 0.19
  258. sensitivity: 1
  259. snap: 0
  260. invert: 1
  261. type: 2
  262. axis: 6
  263. joyNum: 0
  264. - serializedVersion: 3
  265. m_Name: RightStickHorizontal
  266. descriptiveName:
  267. descriptiveNegativeName:
  268. negativeButton:
  269. positiveButton:
  270. altNegativeButton:
  271. altPositiveButton:
  272. gravity: 0
  273. dead: 0.19
  274. sensitivity: 1
  275. snap: 0
  276. invert: 1
  277. type: 2
  278. axis: 3
  279. joyNum: 0
  280. - serializedVersion: 3
  281. m_Name: RightStickVertical
  282. descriptiveName:
  283. descriptiveNegativeName:
  284. negativeButton:
  285. positiveButton:
  286. altNegativeButton:
  287. altPositiveButton:
  288. gravity: 0
  289. dead: 0.19
  290. sensitivity: 1
  291. snap: 0
  292. invert: 1
  293. type: 2
  294. axis: 4
  295. joyNum: 0

附上XBox的按键说明:

测试脚本:

  1. using UnityEngine;
  2. using SK.Framework;
  3. public class Example : MonoBehaviour
  4. {
  5. private void Update()
  6. {
  7. if (Input.GetKeyDown(XBox.A)) Debug.Log("A");
  8. if (Input.GetKeyDown(XBox.B)) Debug.Log("B");
  9. if (Input.GetKeyDown(XBox.X)) Debug.Log("X");
  10. if (Input.GetKeyDown(XBox.Y)) Debug.Log("Y");
  11. if (Input.GetKeyDown(XBox.LB)) Debug.Log("LB");
  12. if (Input.GetKeyDown(XBox.RB)) Debug.Log("RB");
  13. if (Input.GetKeyDown(XBox.View)) Debug.Log("View");
  14. if (Input.GetKeyDown(XBox.Menu)) Debug.Log("Menu");
  15. float lh = Input.GetAxis(XBox.LeftStickHorizontal);
  16. float lv = Input.GetAxis(XBox.LeftStickVertical);
  17. float rh = Input.GetAxis(XBox.RightStickHorizontal);
  18. float rv = Input.GetAxis(XBox.RightStickVertical);
  19. float lt = Input.GetAxis(XBox.LT);
  20. float rt = Input.GetAxis(XBox.RT);
  21. float dPadH = Input.GetAxis(XBox.DPadHorizontal);
  22. float dPadV = Input.GetAxis(XBox.DPadVertical);
  23. if (lh != 0) Debug.Log($"LH:{lh}");
  24. if (lv != 0) Debug.Log($"LV:{lv}");
  25. if (rh != 0) Debug.Log($"RH:{rh}");
  26. if (rv != 0) Debug.Log($"RV:{rv}");
  27. if (lt != 0) Debug.Log($"LT:{lt}");
  28. if (rt != 0) Debug.Log($"RT:{rt}");
  29. if (dPadH != 0) Debug.Log($"DPadH:{dPadH}");
  30. if (dPadV != 0) Debug.Log($"DPadV:{dPadV}");
  31. }
  32. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/146588
推荐阅读
相关标签
  

闽ICP备14008679号