SteamVR_TestController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using Valve.VR;
  6. public class SteamVR_TestController : MonoBehaviour
  7. {
  8. public SteamVR_TestController()
  9. {
  10. EVRButtonId[] array = new EVRButtonId[4];
  11. RuntimeHelpers.InitializeArray(array, fieldof(<PrivateImplementationDetails>.$field-8236C0CC36A2E3B682228FAF16E4A74BE79F3DE1).FieldHandle);
  12. this.buttonIds = array;
  13. this.axisIds = new EVRButtonId[]
  14. {
  15. EVRButtonId.k_EButton_Axis0,
  16. EVRButtonId.k_EButton_Axis1
  17. };
  18. base..ctor();
  19. }
  20. private void OnDeviceConnected(params object[] args)
  21. {
  22. int num = (int)args[0];
  23. CVRSystem system = OpenVR.System;
  24. if (system == null || system.GetTrackedDeviceClass((uint)num) != ETrackedDeviceClass.Controller)
  25. {
  26. return;
  27. }
  28. bool flag = (bool)args[1];
  29. if (flag)
  30. {
  31. Debug.Log(string.Format("Controller {0} connected.", num));
  32. this.PrintControllerStatus(num);
  33. this.controllerIndices.Add(num);
  34. }
  35. else
  36. {
  37. Debug.Log(string.Format("Controller {0} disconnected.", num));
  38. this.PrintControllerStatus(num);
  39. this.controllerIndices.Remove(num);
  40. }
  41. }
  42. private void OnEnable()
  43. {
  44. SteamVR_Utils.Event.Listen("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  45. }
  46. private void OnDisable()
  47. {
  48. SteamVR_Utils.Event.Remove("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  49. }
  50. private void PrintControllerStatus(int index)
  51. {
  52. SteamVR_Controller.Device device = SteamVR_Controller.Input(index);
  53. Debug.Log("index: " + device.index);
  54. Debug.Log("connected: " + device.connected);
  55. Debug.Log("hasTracking: " + device.hasTracking);
  56. Debug.Log("outOfRange: " + device.outOfRange);
  57. Debug.Log("calibrating: " + device.calibrating);
  58. Debug.Log("uninitialized: " + device.uninitialized);
  59. Debug.Log("pos: " + device.transform.pos);
  60. Debug.Log("rot: " + device.transform.rot.eulerAngles);
  61. Debug.Log("velocity: " + device.velocity);
  62. Debug.Log("angularVelocity: " + device.angularVelocity);
  63. int deviceIndex = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost, ETrackedDeviceClass.Controller, 0);
  64. int deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost, ETrackedDeviceClass.Controller, 0);
  65. Debug.Log((deviceIndex != deviceIndex2) ? ((deviceIndex != index) ? "right" : "left") : "first");
  66. }
  67. private void Update()
  68. {
  69. foreach (int num in this.controllerIndices)
  70. {
  71. SteamVR_Overlay instance = SteamVR_Overlay.instance;
  72. if (instance && this.point && this.pointer)
  73. {
  74. SteamVR_Utils.RigidTransform transform = SteamVR_Controller.Input(num).transform;
  75. this.pointer.transform.localPosition = transform.pos;
  76. this.pointer.transform.localRotation = transform.rot;
  77. SteamVR_Overlay.IntersectionResults intersectionResults = default(SteamVR_Overlay.IntersectionResults);
  78. bool flag = instance.ComputeIntersection(transform.pos, transform.rot * Vector3.forward, ref intersectionResults);
  79. if (flag)
  80. {
  81. this.point.transform.localPosition = intersectionResults.point;
  82. this.point.transform.localRotation = Quaternion.LookRotation(intersectionResults.normal);
  83. }
  84. }
  85. else
  86. {
  87. foreach (EVRButtonId evrbuttonId in this.buttonIds)
  88. {
  89. if (SteamVR_Controller.Input(num).GetPressDown(evrbuttonId))
  90. {
  91. Debug.Log(evrbuttonId + " press down");
  92. }
  93. if (SteamVR_Controller.Input(num).GetPressUp(evrbuttonId))
  94. {
  95. Debug.Log(evrbuttonId + " press up");
  96. if (evrbuttonId == EVRButtonId.k_EButton_Axis1)
  97. {
  98. SteamVR_Controller.Input(num).TriggerHapticPulse(500, EVRButtonId.k_EButton_Axis0);
  99. this.PrintControllerStatus(num);
  100. }
  101. }
  102. if (SteamVR_Controller.Input(num).GetPress(evrbuttonId))
  103. {
  104. Debug.Log(evrbuttonId);
  105. }
  106. }
  107. foreach (EVRButtonId evrbuttonId2 in this.axisIds)
  108. {
  109. if (SteamVR_Controller.Input(num).GetTouchDown(evrbuttonId2))
  110. {
  111. Debug.Log(evrbuttonId2 + " touch down");
  112. }
  113. if (SteamVR_Controller.Input(num).GetTouchUp(evrbuttonId2))
  114. {
  115. Debug.Log(evrbuttonId2 + " touch up");
  116. }
  117. if (SteamVR_Controller.Input(num).GetTouch(evrbuttonId2))
  118. {
  119. Vector2 axis = SteamVR_Controller.Input(num).GetAxis(evrbuttonId2);
  120. Debug.Log("axis: " + axis);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. private List<int> controllerIndices = new List<int>();
  127. private EVRButtonId[] buttonIds;
  128. private EVRButtonId[] axisIds;
  129. public Transform point;
  130. public Transform pointer;
  131. }