TestLegacyInput.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SpatialTracking;
  5. using Ximmerse.XR.Utils;
  6. using System.Text;
  7. namespace Ximmerse.XR.tests
  8. {
  9. /// <summary>
  10. /// Test XR controller input interface using legacy UnityEngine.Input class.
  11. /// </summary>
  12. public class TestLegacyInput : MonoBehaviour
  13. {
  14. static readonly string[] xrButtons_Left = new string[]
  15. {
  16. "XRI_Left_PrimaryButton", "XRI_Left_SecondaryButton", "XRI_Left_Primary2DAxisClick",
  17. "XRI_Left_Primary2DAxisTouch","XRI_Left_GripButton", "XRI_Left_TriggerButton", "XRI_Left_MenuButton",
  18. "XRI_Left_Thumbrest",
  19. };
  20. static readonly string[] xrButtons_Right = new string[]
  21. {
  22. "XRI_Right_PrimaryButton", "XRI_Right_SecondaryButton", "XRI_Right_Primary2DAxisClick",
  23. "XRI_Right_Primary2DAxisTouch","XRI_Right_GripButton", "XRI_Right_TriggerButton", "XRI_Right_MenuButton",
  24. "XRI_Right_Thumbrest",
  25. };
  26. static readonly string[] xrAxes_Left = new string[]
  27. {
  28. "XRI_Left_Primary2DAxis_Vertical", "XRI_Left_Primary2DAxis_Horizontal", "XRI_Left_Trigger","XRI_Left_IndexTouch","XRI_Left_Grip"
  29. };
  30. static readonly string[] xrAxes_Right = new string[]
  31. {
  32. "XRI_Right_Primary2DAxis_Vertical", "XRI_Right_Primary2DAxis_Horizontal", "XRI_Right_Trigger","XRI_Right_IndexTouch","XRI_Right_Grip"
  33. };
  34. StringBuilder leftHandMsg = new StringBuilder();
  35. StringBuilder rightHandMsg = new StringBuilder();
  36. void LateUpdate()
  37. {
  38. leftHandMsg.Clear();
  39. rightHandMsg.Clear();
  40. #region Left Hand
  41. foreach (var btn in xrButtons_Left)
  42. {
  43. const string msg = "UnityXRInput - Input.CheckButton: {0} , on : {1}, down : {2}, up : {3}";
  44. bool print = false;
  45. bool on = false, down = false, up = false;
  46. if (Input.GetButton(btn))
  47. {
  48. leftHandMsg.AppendFormat(" {0} ", btn);
  49. print = true;
  50. on = true;
  51. }
  52. if (Input.GetButtonDown(btn))
  53. {
  54. print = true;
  55. down = true;
  56. }
  57. if (Input.GetButtonUp(btn))
  58. {
  59. print = true;
  60. up = true;
  61. }
  62. if (print)
  63. {
  64. Debug.LogFormat(msg, btn, on, down, up);
  65. }
  66. }
  67. foreach (var axis in xrAxes_Left)
  68. {
  69. const string msg = "UnityXRInput - Input.CheckAxes: {0} , value: {1}";
  70. bool print = false;
  71. float val = 0;
  72. if (Input.GetAxis(axis) != 0)
  73. {
  74. val = Input.GetAxis(axis);
  75. print = true;
  76. leftHandMsg.AppendFormat("\r\n {0}={1} ", axis, val.ToString("F1"));
  77. }
  78. if (print)
  79. {
  80. Debug.LogFormat(msg, axis, val);
  81. }
  82. }
  83. #endregion
  84. #region Right Hand
  85. foreach (var btn in xrButtons_Right)
  86. {
  87. const string msg = "UnityXRInput - Input.CheckButton: {0} , on : {1}, down : {2}, up : {3}";
  88. bool print = false;
  89. bool on = false, down = false, up = false;
  90. if (Input.GetButton(btn))
  91. {
  92. rightHandMsg.AppendFormat(" {0} ", btn);
  93. print = true;
  94. on = true;
  95. }
  96. if (Input.GetButtonDown(btn))
  97. {
  98. print = true;
  99. down = true;
  100. }
  101. if (Input.GetButtonUp(btn))
  102. {
  103. print = true;
  104. up = true;
  105. }
  106. if (print)
  107. {
  108. Debug.LogFormat(msg, btn, on, down, up);
  109. }
  110. }
  111. foreach (var axis in xrAxes_Right)
  112. {
  113. const string msg = "UnityXRInput - Input.CheckAxes: {0} , value: {1}";
  114. bool print = false;
  115. float val = 0;
  116. if (Input.GetAxis(axis) != 0)
  117. {
  118. val = Input.GetAxis(axis);
  119. print = true;
  120. rightHandMsg.AppendFormat("\r\n {0}={1} ", axis, val.ToString("F1"));
  121. }
  122. if (print)
  123. {
  124. Debug.LogFormat(msg, axis, val);
  125. }
  126. }
  127. #endregion
  128. if (leftHandMsg.Length > 0)
  129. {
  130. PoseDataSource.GetDataFromSource(TrackedPoseDriver.TrackedPose.LeftPose, out Pose pose);
  131. Matrix4x4 world = Camera.main.transform.parent.localToWorldMatrix * Matrix4x4.TRS(pose.position, pose.rotation, Vector3.one);
  132. RxDraw.Text3D(world.GetColumn(3), Quaternion.LookRotation(Camera.main.transform.forward), 0.01f, leftHandMsg.ToString(), Color.green);
  133. }
  134. if (rightHandMsg.Length > 0)
  135. {
  136. PoseDataSource.GetDataFromSource(TrackedPoseDriver.TrackedPose.RightPose, out Pose pose);
  137. Matrix4x4 world = Camera.main.transform.parent.localToWorldMatrix * Matrix4x4.TRS(pose.position, pose.rotation, Vector3.one);
  138. RxDraw.Text3D(world.GetColumn(3), Quaternion.LookRotation(Camera.main.transform.forward), 0.01f, rightHandMsg.ToString(), Color.green);
  139. }
  140. }
  141. }
  142. }