Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
AudioHapticSource.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using UnityEngine;
7using System.Collections;
8using System.Collections.Generic;
9using System.Linq;
12
13namespace Interhaptics.Utils
14{
15 [AddComponentMenu("Interhaptics/AudioHapticSource")]
16 [RequireComponent(typeof(AudioSource))]
17 public class AudioHapticSource : Internal.HapticSource
18 {
19 // AudioSource variables and properties
20 [SerializeField]
21 public AudioSource audioSource;
22 [SerializeField]
23 private HapticBodyPart[] hapticBodyParts;
24 private bool switchedPlayAtStart = false;
25
26 public bool PlayOnAwake { get { return playAtStart; } set { playAtStart = value; } }
27
28 protected override void Awake()
29 {
30 if (audioSource == null && !TryGetComponent<AudioSource>(out audioSource))
31 {
32 Debug.LogError("AudioSource is not assigned, please assign one");
33 return;
34 }
35 base.Awake();
36 if (audioSource.playOnAwake)
37 {
38 audioSource.playOnAwake = false; // Prevent AudioSource from auto-playing
39 playAtStart = true; // Enable haptic effect to play on start and sync with audio
40 }
41 }
42
43 protected override void Start()
44 {
45 if (playAtStart)
46 {
47 playAtStart = false; //so it doesn't play only vibration on start
48 }
49 AddTarget(hapticBodyParts.Select(hapticBodyPart => new CommandData(Operator.Plus, hapticBodyPart.BodyPart, hapticBodyPart.Side)).ToList());
50 base.Start();
51 if (switchedPlayAtStart)
52 {
53 playAtStart = true;
54 playingCoroutine = StartCoroutine(ControlVibration());
55 }
56 }
57
58 public override void Play()
59 {
60 AddTarget(hapticBodyParts.Select(hapticBodyPart => new CommandData(Operator.Plus, hapticBodyPart.BodyPart, hapticBodyPart.Side)).ToList());
61 base.Play();
62 audioSource.Play();
63 }
64
65 public override void Stop()
66 {
67 base.Stop();
68 audioSource.Stop();
69 RemoveTarget(hapticBodyParts.Select(hapticBodyPart => new CommandData(Operator.Plus, hapticBodyPart.BodyPart, hapticBodyPart.Side)).ToList());
70 }
71
72 // Use the base class's coroutine for looping
73 public override void PlayEventVibration()
74 {
75 if (playingCoroutine != null)
76 {
77 StopCoroutine(playingCoroutine);
78 }
79 playingCoroutine = StartCoroutine(ControlVibration());
80 }
81
82 public override IEnumerator ControlVibration()
83 {
84#if (!ENABLE_METAQUEST && !ENABLE_OPENXR && UNITY_ANDROID && !UNITY_EDITOR) || UNITY_IOS
85 yield return new WaitForSeconds(vibrationOffset);
86#endif
87 DebugMode(string.Format("Started playing haptics! + {0}", Time.time));
88 int loopsPlayed = 0;
89 float loopStartTime = Time.time;
90 float totalTimePlayed = 0f;
91 int maxComputedLoops = maxLoops > 0 ? maxLoops : int.MaxValue;
92
93 while (loopsPlayed < maxComputedLoops)
94 {
95 Play(); // Play audio and haptic effect
96 loopsPlayed++;
97 DebugMode($"Loop {loopsPlayed} start at {Time.time}");
98
99 // Wait for the haptic effect duration to finish before restarting
100 yield return new WaitForSeconds((float)hapticEffectDuration);
101 totalTimePlayed = Time.time - loopStartTime;
102
103 // Check if the maxLoops condition has been met
104 if (loopsPlayed >= maxComputedLoops)
105 {
106 DebugMode($"Max loops reached: {loopsPlayed} loops at {Time.time}");
107 break;
108 }
109 }
110 Stop(); // Stop audio and haptic effect
111 DebugMode($"Finished playing haptics at {Time.time} after {totalTimePlayed} seconds");
112 playingCoroutine = null;
113 }
114 }
115}
void RemoveTarget(List< HapticBodyMapping.CommandData > Target)
Call this method to remove a target from the haptic effect.
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.
void AddTarget(List< HapticBodyMapping.CommandData > Target)
Call this method to add a target to the haptic effect.
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...
override void Stop()
Call this method to stop the haptic effect.
override void Start()
Initialize the haptic effect settings at the start of the game.
override void Play()
Call this method to play the haptic effect.
override IEnumerator ControlVibration()
Controls the vibration perception based on the full length of the haptic material; stops any residual...
override void Awake()
Add the haptic effect file to the when the object is created. The haptic effect file can be in the St...
Operator
Enumeration for operator signs in haptic command data.
Structure for command data in haptic systems.