123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #if UNITY_INPUT_SYSTEM_ENABLE_XR || PACKAGE_DOCS_GENERATION
- using UnityEngine.InputSystem.Controls;
- using UnityEngine.InputSystem.XR.Haptics;
- using UnityEngine.InputSystem.Layouts;
- using UnityEngine.Scripting;
- using UnityEngine.XR;
- namespace UnityEngine.InputSystem.XR
- {
- /// <summary>
- /// The base type of all XR head mounted displays. This can help organize shared behaviour across all HMDs.
- /// </summary>
- [InputControlLayout(isGenericTypeOfDevice = true, displayName = "XR HMD")]
- [Preserve]
- public class XRHMD : TrackedDevice
- {
- [InputControl(noisy = true)]
- [Preserve]
- public Vector3Control leftEyePosition { get; private set; }
- [InputControl(noisy = true)]
- [Preserve]
- public QuaternionControl leftEyeRotation { get; private set; }
- [InputControl(noisy = true)]
- [Preserve]
- public Vector3Control rightEyePosition { get; private set; }
- [InputControl(noisy = true)]
- [Preserve]
- public QuaternionControl rightEyeRotation { get; private set; }
- [InputControl(noisy = true)]
- [Preserve]
- public Vector3Control centerEyePosition { get; private set; }
- [InputControl(noisy = true)]
- [Preserve]
- public QuaternionControl centerEyeRotation { get; private set; }
- protected override void FinishSetup()
- {
- base.FinishSetup();
- centerEyePosition = GetChildControl<Vector3Control>("centerEyePosition");
- centerEyeRotation = GetChildControl<QuaternionControl>("centerEyeRotation");
- leftEyePosition = GetChildControl<Vector3Control>("leftEyePosition");
- leftEyeRotation = GetChildControl<QuaternionControl>("leftEyeRotation");
- rightEyePosition = GetChildControl<Vector3Control>("rightEyePosition");
- rightEyeRotation = GetChildControl<QuaternionControl>("rightEyeRotation");
- }
- }
- /// <summary>
- /// The base type for all XR handed controllers.
- /// </summary>
- [InputControlLayout(commonUsages = new[] { "LeftHand", "RightHand" }, isGenericTypeOfDevice = true, displayName = "XR Controller")]
- [Preserve]
- public class XRController : TrackedDevice
- {
- /// <summary>
- /// A quick accessor for the currently active left handed device.
- /// </summary>
- /// <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>
- public static XRController leftHand => InputSystem.GetDevice<XRController>(CommonUsages.LeftHand);
- /// <summary>
- /// A quick accessor for the currently active right handed device. This is also tracked via usages on the device.
- /// </summary>
- /// <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>
- public static XRController rightHand => InputSystem.GetDevice<XRController>(CommonUsages.RightHand);
- protected override void FinishSetup()
- {
- base.FinishSetup();
- var capabilities = description.capabilities;
- var deviceDescriptor = XRDeviceDescriptor.FromJson(capabilities);
- if (deviceDescriptor != null)
- {
- #if UNITY_2019_3_OR_NEWER
- if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Left) != 0)
- InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
- else if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Right) != 0)
- InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
- #else
- if (deviceDescriptor.deviceRole == InputDeviceRole.LeftHanded)
- InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
- else if (deviceDescriptor.deviceRole == InputDeviceRole.RightHanded)
- InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
- #endif //UNITY_2019_3_OR_NEWER
- }
- }
- }
- /// <summary>
- /// Identifies a controller that is capable of rumble or haptics.
- /// </summary>
- [Preserve]
- public class XRControllerWithRumble : XRController
- {
- public void SendImpulse(float amplitude, float duration)
- {
- var command = SendHapticImpulseCommand.Create(0, amplitude, duration);
- ExecuteCommand(ref command);
- }
- }
- }
- #endif
|