6 #if !UNITY_2017_2_OR_NEWER 12 using System.Collections.Generic;
13 using Windows.Devices.Haptics;
14 using Windows.Foundation;
15 using Windows.Perception;
16 using Windows.Storage.Streams;
17 using Windows.UI.Input.Spatial;
18 #elif UNITY_EDITOR_WIN 19 using System.Runtime.InteropServices;
31 #if UNITY_2017_2_OR_NEWER 32 #if UNITY_EDITOR_WIN && UNITY_WSA 33 [DllImport(
"EditorMotionController")]
34 private static extern bool StartHaptics([In] uint controllerId, [In]
float intensity, [In]
float durationInSeconds);
36 [DllImport(
"EditorMotionController")]
37 private static extern bool StopHaptics([In] uint controllerId);
38 #endif // UNITY_EDITOR_WIN && UNITY_WSA 41 private const ushort ContinuousBuzzWaveform = 0x1004;
44 public static void StartHaptics(
this InteractionSource interactionSource,
float intensity)
46 interactionSource.StartHaptics(intensity,
float.MaxValue);
49 public static void StartHaptics(
this InteractionSource interactionSource,
float intensity,
float durationInSeconds)
59 IReadOnlyList<SpatialInteractionSourceState> sources = SpatialInteractionManager.GetForCurrentView().GetDetectedSourcesAtTimestamp(PerceptionTimestampHelper.FromHistoricalTargetTime(DateTimeOffset.Now));
61 foreach (SpatialInteractionSourceState sourceState
in sources)
63 if (sourceState.Source.Id.Equals(interactionSource.id))
65 SimpleHapticsController simpleHapticsController = sourceState.Source.Controller.SimpleHapticsController;
66 foreach (SimpleHapticsControllerFeedback hapticsFeedback
in simpleHapticsController.SupportedFeedback)
68 if (hapticsFeedback.Waveform.Equals(ContinuousBuzzWaveform))
70 if (durationInSeconds.Equals(
float.MaxValue))
72 simpleHapticsController.SendHapticFeedback(hapticsFeedback, intensity);
76 simpleHapticsController.SendHapticFeedbackForDuration(hapticsFeedback, intensity, TimeSpan.FromSeconds(durationInSeconds));
84 #elif UNITY_EDITOR_WIN 85 StartHaptics(interactionSource.id, intensity, durationInSeconds);
86 #endif // !UNITY_EDITOR 89 public static void StopHaptics(
this InteractionSource interactionSource)
99 IReadOnlyList<SpatialInteractionSourceState> sources = SpatialInteractionManager.GetForCurrentView().GetDetectedSourcesAtTimestamp(PerceptionTimestampHelper.FromHistoricalTargetTime(DateTimeOffset.Now));
101 foreach (SpatialInteractionSourceState sourceState
in sources)
103 if (sourceState.Source.Id.Equals(interactionSource.id))
105 sourceState.Source.Controller.SimpleHapticsController.StopFeedback();
109 #elif UNITY_EDITOR_WIN 110 StopHaptics(interactionSource.id);
111 #endif // !UNITY_EDITOR 115 public static IAsyncOperation<IRandomAccessStreamWithContentType> TryGetRenderableModelAsync(
this InteractionSource interactionSource)
117 IAsyncOperation<IRandomAccessStreamWithContentType> returnValue = null;
123 IReadOnlyList<SpatialInteractionSourceState> sources = SpatialInteractionManager.GetForCurrentView().GetDetectedSourcesAtTimestamp(PerceptionTimestampHelper.FromHistoricalTargetTime(DateTimeOffset.Now));
125 foreach (SpatialInteractionSourceState sourceState
in sources)
127 if (sourceState.Source.Id.Equals(interactionSource.id))
129 returnValue = sourceState.Source.Controller.TryGetRenderableModelAsync();
137 #endif // !UNITY_EDITOR 139 #endif // UNITY_2017_2_OR_NEWER