GenericXRDevice.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if UNITY_INPUT_SYSTEM_ENABLE_XR || PACKAGE_DOCS_GENERATION
  2. using UnityEngine.InputSystem.Controls;
  3. using UnityEngine.InputSystem.XR.Haptics;
  4. using UnityEngine.InputSystem.Layouts;
  5. using UnityEngine.Scripting;
  6. using UnityEngine.XR;
  7. namespace UnityEngine.InputSystem.XR
  8. {
  9. /// <summary>
  10. /// The base type of all XR head mounted displays. This can help organize shared behaviour across all HMDs.
  11. /// </summary>
  12. [InputControlLayout(isGenericTypeOfDevice = true, displayName = "XR HMD")]
  13. [Preserve]
  14. public class XRHMD : TrackedDevice
  15. {
  16. [InputControl(noisy = true)]
  17. [Preserve]
  18. public Vector3Control leftEyePosition { get; private set; }
  19. [InputControl(noisy = true)]
  20. [Preserve]
  21. public QuaternionControl leftEyeRotation { get; private set; }
  22. [InputControl(noisy = true)]
  23. [Preserve]
  24. public Vector3Control rightEyePosition { get; private set; }
  25. [InputControl(noisy = true)]
  26. [Preserve]
  27. public QuaternionControl rightEyeRotation { get; private set; }
  28. [InputControl(noisy = true)]
  29. [Preserve]
  30. public Vector3Control centerEyePosition { get; private set; }
  31. [InputControl(noisy = true)]
  32. [Preserve]
  33. public QuaternionControl centerEyeRotation { get; private set; }
  34. protected override void FinishSetup()
  35. {
  36. base.FinishSetup();
  37. centerEyePosition = GetChildControl<Vector3Control>("centerEyePosition");
  38. centerEyeRotation = GetChildControl<QuaternionControl>("centerEyeRotation");
  39. leftEyePosition = GetChildControl<Vector3Control>("leftEyePosition");
  40. leftEyeRotation = GetChildControl<QuaternionControl>("leftEyeRotation");
  41. rightEyePosition = GetChildControl<Vector3Control>("rightEyePosition");
  42. rightEyeRotation = GetChildControl<QuaternionControl>("rightEyeRotation");
  43. }
  44. }
  45. /// <summary>
  46. /// The base type for all XR handed controllers.
  47. /// </summary>
  48. [InputControlLayout(commonUsages = new[] { "LeftHand", "RightHand" }, isGenericTypeOfDevice = true, displayName = "XR Controller")]
  49. [Preserve]
  50. public class XRController : TrackedDevice
  51. {
  52. /// <summary>
  53. /// A quick accessor for the currently active left handed device.
  54. /// </summary>
  55. /// <remarks>If there is no left hand connected, this will be null. This also matches any currently tracked device that contains the 'LeftHand' device usage.</remarks>
  56. public static XRController leftHand => InputSystem.GetDevice<XRController>(CommonUsages.LeftHand);
  57. /// <summary>
  58. /// A quick accessor for the currently active right handed device. This is also tracked via usages on the device.
  59. /// </summary>
  60. /// <remarks>If there is no left hand connected, this will be null. This also matches any currently tracked device that contains the 'RightHand' device usage.</remarks>
  61. public static XRController rightHand => InputSystem.GetDevice<XRController>(CommonUsages.RightHand);
  62. protected override void FinishSetup()
  63. {
  64. base.FinishSetup();
  65. var capabilities = description.capabilities;
  66. var deviceDescriptor = XRDeviceDescriptor.FromJson(capabilities);
  67. if (deviceDescriptor != null)
  68. {
  69. #if UNITY_2019_3_OR_NEWER
  70. if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Left) != 0)
  71. InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
  72. else if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Right) != 0)
  73. InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
  74. #else
  75. if (deviceDescriptor.deviceRole == InputDeviceRole.LeftHanded)
  76. InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
  77. else if (deviceDescriptor.deviceRole == InputDeviceRole.RightHanded)
  78. InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
  79. #endif //UNITY_2019_3_OR_NEWER
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Identifies a controller that is capable of rumble or haptics.
  85. /// </summary>
  86. [Preserve]
  87. public class XRControllerWithRumble : XRController
  88. {
  89. public void SendImpulse(float amplitude, float duration)
  90. {
  91. var command = SendHapticImpulseCommand.Create(0, amplitude, duration);
  92. ExecuteCommand(ref command);
  93. }
  94. }
  95. }
  96. #endif