分享

Unity 关于手柄摇杆和键值的测试

 勤奋不止 2019-09-29

之前的关于手柄的帖子有网友说测试的不准,所以今天写了一个测试键值的脚本,希望能帮到各位

下面是关于测试的代码,里面加了注释:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. /// <summary>
  5. /// 测试游戏手柄键值
  6. /// </summary>
  7. public class PlayerJoystickClass : MonoBehaviour
  8. {
  9. private string currentButton;//当前按下的按键
  10. private string currentAxis;//当前移动的轴向
  11. private float axisInput;//当前轴向的值
  12. void Update()
  13. {
  14. getAxis();
  15. getButtons();
  16. }
  17. /// <summary>
  18. /// Get Button data of the joysick
  19. /// </summary>
  20. void getButtons()
  21. {
  22. var values = Enum.GetValues(typeof(KeyCode));//存储所有的按键
  23. for (int x = 0; x < values.Length; x++)
  24. {
  25. if (Input.GetKeyDown((KeyCode)values.GetValue(x)))
  26. {
  27. currentButton = values.GetValue(x).ToString();//遍历并获取当前按下的按键
  28. }
  29. }
  30. }
  31. /// <summary>
  32. /// Get Axis data of the joysick
  33. /// </summary>
  34. void getAxis()
  35. {
  36. if (Input.GetAxisRaw("X axis") > 0.3 || Input.GetAxisRaw("X axis") < -0.3)
  37. {
  38. currentAxis = "X axis";
  39. axisInput = Input.GetAxisRaw("X axis");
  40. }
  41. if (Input.GetAxisRaw("Y axis") > 0.3 || Input.GetAxisRaw("Y axis") < -0.3)
  42. {
  43. currentAxis = "Y axis";
  44. axisInput = Input.GetAxisRaw("Y axis");
  45. }
  46. if (Input.GetAxisRaw("3rd axis") > 0.3 || Input.GetAxisRaw("3rd axis") < -0.3)
  47. {
  48. currentAxis = "3rd axis";
  49. axisInput = Input.GetAxisRaw("3rd axis");
  50. }
  51. if (Input.GetAxisRaw("4th axis") > 0.3 || Input.GetAxisRaw("4th axis") < -0.3)
  52. {
  53. currentAxis = "4th axis";
  54. axisInput = Input.GetAxisRaw("4th axis");
  55. }
  56. if (Input.GetAxisRaw("5th axis") > 0.3 || Input.GetAxisRaw("5th axis") < -0.3)
  57. {
  58. currentAxis = "5th axis";
  59. axisInput = Input.GetAxisRaw("5th axis");
  60. }
  61. if (Input.GetAxisRaw("6th axis") > 0.3 || Input.GetAxisRaw("6th axis") < -0.3)
  62. {
  63. currentAxis = "6th axis";
  64. axisInput = Input.GetAxisRaw("6th axis");
  65. }
  66. if (Input.GetAxisRaw("7th axis") > 0.3 || Input.GetAxisRaw("7th axis") < -0.3)
  67. {
  68. currentAxis = "7th axis";
  69. axisInput = Input.GetAxisRaw("7th axis");
  70. }
  71. if (Input.GetAxisRaw("8th axis") > 0.3 || Input.GetAxisRaw("8th axis") < -0.3)
  72. {
  73. currentAxis = "8th axis";
  74. axisInput = Input.GetAxisRaw("8th axis");
  75. }
  76. if (Input.GetAxisRaw("9th axis") > 0.3 || Input.GetAxisRaw("9th axis") < -0.3)
  77. {
  78. currentAxis = "9th axis";
  79. axisInput = Input.GetAxisRaw("9th axis");
  80. }
  81. if (Input.GetAxisRaw("10th axis") > 0.3 || Input.GetAxisRaw("10th axis") < -0.3)
  82. {
  83. currentAxis = "10th axis";
  84. axisInput = Input.GetAxisRaw("10th axis");
  85. }
  86. if (Input.GetAxisRaw("11th axis") > 0.3 || Input.GetAxisRaw("11th axis") < -0.3)
  87. {
  88. currentAxis = "11th axis";
  89. axisInput = Input.GetAxisRaw("11th axis");
  90. }
  91. if (Input.GetAxisRaw("12th axis") > 0.3 || Input.GetAxisRaw("12th axis") < -0.3)
  92. {
  93. currentAxis = "12th axis";
  94. axisInput = Input.GetAxisRaw("12th axis");
  95. }
  96. if (Input.GetAxisRaw("13th axis") > 0.3 || Input.GetAxisRaw("13th axis") < -0.3)
  97. {
  98. currentAxis = "13th axis";
  99. axisInput = Input.GetAxisRaw("13th axis");
  100. }
  101. if (Input.GetAxisRaw("14th axis") > 0.3 || Input.GetAxisRaw("14th axis") < -0.3)
  102. {
  103. currentAxis = "14th axis";
  104. axisInput = Input.GetAxisRaw("14th axis");
  105. }
  106. }
  107. /// <summary>
  108. /// show the data onGUI
  109. /// </summary>
  110. void OnGUI()
  111. {
  112. GUI.TextArea(new Rect(0, 0, 250, 50), "Current Button : " + currentButton);//使用GUI在屏幕上面实时打印当前按下的按键
  113. GUI.TextArea(new Rect(0, 100, 250, 50), "Current Axis : " + currentAxis);//使用GUI在屏幕上面实时打印当前按下的轴
  114. GUI.TextArea(new Rect(0, 200, 250, 50), "Axis Value : " + axisInput);//使用GUI在屏幕上面实时打印当前按下的轴的量
  115. }
  116. }

连接手柄后,当你按下手柄的键位和轴向时,都会在屏幕中输出出来,使用的话调用Input的API就可以了


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多