当前位置:   article > 正文

Quest2 在Unity中获取手柄的输入,以及检测各个按键是否被按下,以及控制手柄振动功能_quest 2 手柄 unity

quest 2 手柄 unity
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR;
  5. public class Xr_input_Record : MonoBehaviour
  6. {
  7. //private UnityEngine.XR.InputDevice device;
  8. // Start is called before the first frame update
  9. public UnityEngine.XR.InputDevice leftHand_device;
  10. public float 振幅=0.5f;
  11. public uint 频道=0;
  12. public float 持续时长=1f;
  13. void Start()
  14. {
  15. // Allinput_device_record();
  16. // get_left_hand();
  17. }
  18. private void OnEnable() {
  19. // Allinput_device_record();
  20. // get_left_hand();
  21. // display_device_featureName();
  22. get_left_hand();
  23. 控制器振动功能();
  24. }
  25. public void Allinput_device_record()
  26. {
  27. var inputDevices = new List<UnityEngine.XR.InputDevice>();
  28. UnityEngine.XR.InputDevices.GetDevices(inputDevices);
  29. foreach (var device_a in inputDevices)
  30. {
  31. Debug.Log(string.Format("设备名字 '{0}' 设备角色 '{1}'", device_a.name, device_a.role.ToString()));
  32. }
  33. }
  34. public void get_left_hand()
  35. {
  36. var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
  37. var desiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller;
  38. UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, leftHandedControllers);
  39. foreach (var device in leftHandedControllers)
  40. {
  41. Debug.Log(string.Format("左手设备名 '{0}' 该设备具有的特征 '{1}'", device.name, device.characteristics.ToString()));
  42. leftHand_device=device;
  43. }
  44. }
  45. public void display_device_featureName()
  46. {
  47. var inputFeatures = new List<UnityEngine.XR.InputFeatureUsage>();
  48. if (leftHand_device.TryGetFeatureUsages(inputFeatures)) //用于获取设备的使用表格
  49. {
  50. foreach (var feature in inputFeatures)
  51. {
  52. if (feature.type == typeof(bool))
  53. {
  54. bool featureValue;
  55. if (leftHand_device.TryGetFeatureValue(feature.As<bool>(), out featureValue))
  56. {
  57. Debug.Log(string.Format("功能名字 {0}'s 功能的值 {1}", feature.name, featureValue.ToString()));
  58. }
  59. }
  60. }
  61. }
  62. }
  63. public void update_inputDynamic_record()
  64. {
  65. bool triggerValue;
  66. //知道按键名字,直接根据名字获得相对应的值
  67. if (leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out triggerValue) && triggerValue)
  68. {
  69. Debug.Log("按下grip button");
  70. }
  71. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.menuButton, out triggerValue) && triggerValue)
  72. {
  73. Debug.Log("按下menu button");
  74. }
  75. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out triggerValue) && triggerValue)
  76. {
  77. Debug.Log("按下Primary button");
  78. }
  79. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryTouch, out triggerValue) && triggerValue)
  80. {
  81. Debug.Log("按下Primary touch");
  82. }
  83. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out triggerValue) && triggerValue)
  84. {
  85. Debug.Log("按下second button");
  86. }
  87. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryTouch, out triggerValue) && triggerValue)
  88. {
  89. Debug.Log("按下second touch");
  90. }
  91. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue) && triggerValue)
  92. {
  93. Debug.Log("按下trigger button");
  94. }
  95. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisClick, out triggerValue) && triggerValue)
  96. {
  97. Debug.Log("按下primary 2D click button");
  98. }
  99. else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisTouch, out triggerValue) && triggerValue)
  100. {
  101. Debug.Log("按下primary 2D click touch");
  102. }
  103. /*else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.isTracked, out triggerValue) && triggerValue)
  104. {
  105. Debug.Log("按下is_tracked touch");
  106. }*/
  107. }
  108. public void 控制器振动功能()
  109. {
  110. // List<UnityEngine.XR.InputDevice> devices = new List<UnityEngine.XR.InputDevice>();
  111. print("A1");
  112. UnityEngine.XR.HapticCapabilities capabilities;
  113. if (leftHand_device.TryGetHapticCapabilities(out capabilities))
  114. {
  115. print("A2");
  116. if (capabilities.supportsImpulse)
  117. {
  118. print("A3");
  119. uint channel = 0;
  120. // float amplitude = 5f;
  121. float duration = 5.0f;
  122. leftHand_device.SendHapticImpulse(频道, 振幅, 持续时长);
  123. }
  124. }
  125. }
  126. // Update is called once per frame
  127. void Update()
  128. {
  129. update_inputDynamic_record();
  130. }
  131. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/99949
推荐阅读
相关标签
  

闽ICP备14008679号