/// <summary> /// Start haptic feedback on the interaction source with the specified intensity and continue for the specified amount of time. /// </summary> /// <param name="interactionSource">The source to start haptics on.</param> /// <param name="intensity">The strength of the haptic feedback from 0.0 (no haptics) to 1.0 (maximum strength).</param> /// <param name="durationInSeconds">The time period expressed in seconds.</param> public static bool StartHaptics(this InteractionSource interactionSource, float intensity, float durationInSeconds) { if (!IsHapticsAvailable) { return(false); } #if WINDOWS_UWP || DOTNETWINRT_PRESENT SimpleHapticsController simpleHapticsController = interactionSource.GetSpatialInteractionSource()?.Controller.SimpleHapticsController; foreach (SimpleHapticsControllerFeedback hapticsFeedback in simpleHapticsController?.SupportedFeedback) { if (hapticsFeedback.Waveform.Equals(ContinuousBuzzWaveform)) { if (UnityEngine.Mathf.Approximately(durationInSeconds, float.MaxValue)) { simpleHapticsController.SendHapticFeedback(hapticsFeedback, intensity); } else { simpleHapticsController.SendHapticFeedbackForDuration(hapticsFeedback, intensity, TimeSpan.FromSeconds(durationInSeconds)); } return(true); } } #endif // WINDOWS_UWP || DOTNETWINRT_PRESENT return(false); }
public static void StartHaptics(this InteractionSource interactionSource, float intensity, float durationInSeconds) { #if !UNITY_EDITOR UnityEngine.WSA.Application.InvokeOnUIThread(() => { IReadOnlyList <SpatialInteractionSourceState> sources = SpatialInteractionManager.GetForCurrentView().GetDetectedSourcesAtTimestamp(PerceptionTimestampHelper.FromHistoricalTargetTime(DateTimeOffset.Now)); foreach (SpatialInteractionSourceState sourceState in sources) { if (sourceState.Source.Id.Equals(interactionSource.id)) { SimpleHapticsController simpleHapticsController = sourceState.Source.Controller.SimpleHapticsController; foreach (SimpleHapticsControllerFeedback hapticsFeedback in simpleHapticsController.SupportedFeedback) { if (hapticsFeedback.Waveform.Equals(ContinuousBuzzWaveform)) { if (durationInSeconds.Equals(float.MaxValue)) { simpleHapticsController.SendHapticFeedback(hapticsFeedback, intensity); } else { simpleHapticsController.SendHapticFeedbackForDuration(hapticsFeedback, intensity, TimeSpan.FromSeconds(durationInSeconds)); } return; } } } } }, true); #endif }
protected override IEnumerator VibrateCoroutine(long milliseconds, float amplitude) { var start = DatTime.Now; var seconds = Units.Milliseconds.Seconds(milliseconds); var ts = TimeSpan.FromSeconds(seconds); if (expressions.ContainsKey(KnownSimpleHapticsControllerWaveforms.BuzzContinuous)) { controller?.SendHapticFeedbackForDuration( expressions[KnownSimpleHapticsControllerWaveforms.BuzzContinuous], amplitude, ts); } while ((DateTime.Now - start) < ts) { yield return(null); } }