No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SendHapticImpulseCommand.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
  2. #if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) || PACKAGE_DOCS_GENERATION
  3. using System.Runtime.InteropServices;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.Utilities;
  6. namespace UnityEngine.InputSystem.XR.Haptics
  7. {
  8. /// <summary>
  9. /// A device command sent to a device to set it's motor rumble amplitude for a set duration.
  10. /// </summary>
  11. /// <remarks>This is directly used by the <see cref="XRControllerWithRumble"/> class. For clearer details of using this command, see that class.</remarks>
  12. [StructLayout(LayoutKind.Explicit, Size = kSize)]
  13. public struct SendHapticImpulseCommand : IInputDeviceCommandInfo
  14. {
  15. static FourCC Type => new FourCC('X', 'H', 'I', '0');
  16. private const int kSize = InputDeviceCommand.kBaseCommandSize + sizeof(int) + (sizeof(float) * 2);
  17. [FieldOffset(0)]
  18. InputDeviceCommand baseCommand;
  19. [FieldOffset(InputDeviceCommand.kBaseCommandSize)]
  20. private int channel;
  21. [FieldOffset(InputDeviceCommand.kBaseCommandSize + sizeof(int))]
  22. private float amplitude;
  23. [FieldOffset(InputDeviceCommand.kBaseCommandSize + sizeof(int) + (sizeof(float)))]
  24. private float duration;
  25. public FourCC typeStatic => Type;
  26. /// <summary>
  27. /// Creates a device command that can then be sent to a specific device.
  28. /// </summary>
  29. /// <param name="motorChannel">The desired motor you want to rumble</param>
  30. /// <param name="motorAmplitude">The desired motor amplitude that should be within a [0-1] range.</param>
  31. /// <param name="motorDuration">The desired duration of the impulse in seconds.</param>
  32. /// <returns>The command that should be sent to the device via <c>InputDevice.ExecuteCommand</c>.</returns>
  33. public static SendHapticImpulseCommand Create(int motorChannel, float motorAmplitude, float motorDuration)
  34. {
  35. return new SendHapticImpulseCommand
  36. {
  37. baseCommand = new InputDeviceCommand(Type, kSize),
  38. channel = motorChannel,
  39. amplitude = motorAmplitude,
  40. duration = motorDuration
  41. };
  42. }
  43. }
  44. }
  45. #endif