Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
Interhaptics.Internal.HapticSource Class Reference
Inheritance diagram for Interhaptics.Internal.HapticSource:
Interhaptics.SpatialHapticSource Interhaptics.Utils.AudioHapticSource Interhaptics.Utils.EventHapticSource Interhaptics.Utils.ParametricHapticSource Interhaptics.Samples.XRControllerHapticSource

Public Member Functions

virtual void ApplyTargetIntensity ()
 
void ApplySourceIntensity ()
 Call this method to apply the source intensity.
 
void ApplyLooping (int loopValue)
 Call this method to apply the looping state.
 
void DebugMode (string debugMessage)
 Debug method to print messages in the console only when debugMode is enabled.
 
virtual void Play ()
 Call this method to play the haptic effect.
 
virtual void Stop ()
 Call this method to stop the haptic effect.
 
void AddTarget (List< HapticBodyMapping.CommandData > Target)
 Call this method to add a target to the haptic effect.
 
void RemoveTarget (List< HapticBodyMapping.CommandData > Target)
 Call this method to remove a target from the haptic effect.
 
virtual void PlayEventVibration ()
 Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibrationOffset.
 
virtual IEnumerator ControlVibration ()
 Controls the vibration perception based on the full length of the haptic material; stops any residual haptics which might come from the controller after the haptic playback length (made for platforms other than mobile)
 

Public Attributes

HapticMaterial hapticMaterial
 
bool useStreamingAssets = false
 
string hapticEffectStreamingAssetsPath
 
float vibrationOffset
 
bool debugMode
 
bool isLooping
 
int maxLoops = 1
 
float maxLoopTime = 10f
 
double targetIntensity = 1.0
 
bool playAtStart = false
 
bool isPlaying = false
 
double hapticEffectDuration = 0
 
Coroutine playingCoroutine = null
 
double loopStartTime = 0
 

Static Public Attributes

const string ERROR_MESSAGE_MONO = "Interhaptics requires IL2CPP scripting backend for Android. Please change it in Player Settings. Haptics will not play on the Mono scripting backend on the Android platform."
 

Protected Member Functions

virtual void Awake ()
 Add the haptic effect file to the when the object is created. The haptic effect file can be in the StreamingAssets folder if the useStreamingAssets property is set to true.
 
virtual void Start ()
 Initialize the haptic effect settings at the start of the game.
 
virtual void Update ()
 Update the haptic effect settings at every frame.
 

Properties

int HapticMaterialId [get, set]
 
float SourceIntensity [get, set]
 

Detailed Description

Definition at line 17 of file HapticSource.cs.

Member Function Documentation

◆ AddTarget()

◆ ApplyLooping()

void Interhaptics.Internal.HapticSource.ApplyLooping ( int loopValue)

Call this method to apply the looping state.

Definition at line 192 of file HapticSource.cs.

193 {
194 DebugMode("Applied looping: " + loopValue);
195 HAR.SetEventLoop(HapticMaterialId, loopValue);
196 }
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.

References Interhaptics.Internal.HapticSource.DebugMode(), and Interhaptics.Internal.HapticSource.HapticMaterialId.

Referenced by Interhaptics.Internal.HapticSource.ControlVibration().

◆ ApplySourceIntensity()

void Interhaptics.Internal.HapticSource.ApplySourceIntensity ( )

Call this method to apply the source intensity.

Definition at line 185 of file HapticSource.cs.

186 {
187 HAR.SetEventIntensity(HapticMaterialId, sourceIntensity);
188 }

References Interhaptics.Internal.HapticSource.HapticMaterialId.

Referenced by Interhaptics.Samples.SourceIntensitySlider.HandleSliderValueChanged(), and Interhaptics.Internal.HapticSource.Update().

◆ ApplyTargetIntensity()

virtual void Interhaptics.Internal.HapticSource.ApplyTargetIntensity ( )
virtual

Definition at line 177 of file HapticSource.cs.

178 {
179 //Empty for now
180 }

◆ Awake()

virtual void Interhaptics.Internal.HapticSource.Awake ( )
protectedvirtual

Add the haptic effect file to the when the object is created. The haptic effect file can be in the StreamingAssets folder if the useStreamingAssets property is set to true.

Reimplemented in Interhaptics.Utils.AudioHapticSource, and Interhaptics.Utils.ParametricHapticSource.

Definition at line 86 of file HapticSource.cs.

87 {
88#if UNITY_ANDROID
89 if (HapticManager.MonoScriptingBackend)
90 {
91 DebugMode(ERROR_MESSAGE_MONO + "Haptic Source");
92 return;
93 }
94 else
95 {
96 DebugMode("IL2CPP Haptic Source");
97 }
98#endif
100 {
101 hapticEffectStreamingAssetsPath = Path.Combine(Application.streamingAssetsPath, hapticEffectStreamingAssetsPath);
102 if (File.Exists(hapticEffectStreamingAssetsPath))
103 {
104 HapticMaterialId = Core.HAR.AddHMString(File.ReadAllText(hapticEffectStreamingAssetsPath));
105 DebugMode("Loaded haptic material from StreamingAssets: " + hapticEffectStreamingAssetsPath);
106 }
107 else
108 {
109 DebugMode("Haptic material file not found in StreamingAssets: " + hapticEffectStreamingAssetsPath);
110 }
111 }
112 else if (hapticMaterial != null)
113 {
114 HapticMaterialId = Core.HAR.AddHM(hapticMaterial);
115 }
116 else
117 {
118 DebugMode("No haptic effect provided. Please assign a HapticMaterial in the inspector or provide a path to a haptic effect file in the StreamingAssets directory.");
119 }
120 }

References Interhaptics.Internal.HapticSource.DebugMode(), Interhaptics.Internal.HapticSource.ERROR_MESSAGE_MONO, Interhaptics.Internal.HapticSource.hapticEffectStreamingAssetsPath, Interhaptics.Internal.HapticSource.hapticMaterial, Interhaptics.Internal.HapticSource.HapticMaterialId, and Interhaptics.Internal.HapticSource.useStreamingAssets.

◆ ControlVibration()

virtual IEnumerator Interhaptics.Internal.HapticSource.ControlVibration ( )
virtual

Controls the vibration perception based on the full length of the haptic material; stops any residual haptics which might come from the controller after the haptic playback length (made for platforms other than mobile)

Returns

Reimplemented in Interhaptics.SpatialHapticSource, and Interhaptics.Utils.AudioHapticSource.

Definition at line 279 of file HapticSource.cs.

280 {
281 DebugMode(string.Format("Started playing haptics! + {0}", Time.time));
282
283 // Coroutine logic for other platforms
284 if (isLooping)
285 {
287 }
288 Play();
289 float effectStartTime = Time.time;
290 float effectEndTime = effectStartTime + maxLoopTime;
291 // Check if we have a valid maxLoopTime, otherwise keep playing indefinitely
292 if (maxLoopTime > 0)
293 {
294 // Loop until the maxLoopTime is reached
295 while (Time.time < effectEndTime)
296 {
297 yield return null; // Wait until the next frame
298 }
299 // Once the maxLoopTime is exceeded, stop the effect
300 DebugMode($"Stopped playing haptics at {Time.time} after reaching max loop time of {maxLoopTime} seconds");
301 if (isLooping)
302 {
303 ApplyLooping(0);
304 }
305 isPlaying = false;
306 }
307 else
308 {
309 // If maxLoopTime is not valid (e.g., -1 for indefinite), just wait for the duration of the effect
310 yield return new WaitForSeconds((float)hapticEffectDuration);
311 }
312 // Clean up
313 playingCoroutine = null;
314 }
virtual void Play()
Call this method to play the haptic effect.
void ApplyLooping(int loopValue)
Call this method to apply the looping state.

References Interhaptics.Internal.HapticSource.ApplyLooping(), Interhaptics.Internal.HapticSource.DebugMode(), Interhaptics.Internal.HapticSource.hapticEffectDuration, Interhaptics.Internal.HapticSource.isLooping, Interhaptics.Internal.HapticSource.isPlaying, Interhaptics.Internal.HapticSource.maxLoops, Interhaptics.Internal.HapticSource.maxLoopTime, Interhaptics.Internal.HapticSource.Play(), and Interhaptics.Internal.HapticSource.playingCoroutine.

Referenced by Interhaptics.Internal.HapticSource.PlayEventVibration().

◆ DebugMode()

◆ Play()

◆ PlayEventVibration()

virtual void Interhaptics.Internal.HapticSource.PlayEventVibration ( )
virtual

Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibrationOffset.

Reimplemented in Interhaptics.SpatialHapticSource, Interhaptics.Utils.AudioHapticSource, Interhaptics.Utils.EventHapticSource, and Interhaptics.Utils.ParametricHapticSource.

Definition at line 256 of file HapticSource.cs.

257 {
258#if (!ENABLE_METAQUEST && !ENABLE_OPENXR && UNITY_ANDROID && !UNITY_EDITOR) || UNITY_IOS // Coroutine logic specific to Android and iOS
259 float loopStartTime = Time.time;
260 int maxComputedLoops = maxLoops > 0 ? maxLoops : int.MaxValue;
261 DebugMode("Playing Haptic Material id:" + HapticMaterialId);
262 HAR.PlayHapticEffectId(HapticMaterialId, 1, maxComputedLoops, vibrationOffset);
263 isPlaying = false;
264 float totalTimePlayed = maxComputedLoops * (float)hapticEffectDuration;
265 DebugMode($"Finished playing haptics at {Time.time} after {totalTimePlayed} seconds");
266#else
267 if (playingCoroutine != null)
268 {
269 StopCoroutine(playingCoroutine);
270 }
271 playingCoroutine = StartCoroutine(ControlVibration());
272#endif
273 }
virtual IEnumerator ControlVibration()
Controls the vibration perception based on the full length of the haptic material; stops any residual...

References Interhaptics.Internal.HapticSource.ControlVibration(), Interhaptics.Internal.HapticSource.DebugMode(), Interhaptics.Internal.HapticSource.hapticEffectDuration, Interhaptics.Internal.HapticSource.HapticMaterialId, Interhaptics.Internal.HapticSource.isPlaying, Interhaptics.Internal.HapticSource.loopStartTime, Interhaptics.Internal.HapticSource.maxLoops, Interhaptics.Internal.HapticSource.playingCoroutine, and Interhaptics.Internal.HapticSource.vibrationOffset.

Referenced by Interhaptics.Internal.HapticSource.Start().

◆ RemoveTarget()

void Interhaptics.Internal.HapticSource.RemoveTarget ( List< HapticBodyMapping.CommandData > Target)

Call this method to remove a target from the haptic effect.

Parameters
Target

Definition at line 249 of file HapticSource.cs.

250 {
251 Core.HAR.RemoveTargetFromEvent(HapticMaterialId, Target);
252 }

References Interhaptics.Internal.HapticSource.HapticMaterialId.

Referenced by Interhaptics.Utils.AudioHapticSource.Stop().

◆ Start()

virtual void Interhaptics.Internal.HapticSource.Start ( )
protectedvirtual

Initialize the haptic effect settings at the start of the game.

Reimplemented in Interhaptics.Samples.XRControllerHapticSource, Interhaptics.SpatialHapticSource, Interhaptics.Utils.AudioHapticSource, Interhaptics.Utils.EventHapticSource, and Interhaptics.Utils.ParametricHapticSource.

Definition at line 125 of file HapticSource.cs.

126 {
127 // Initialize the haptic effect at the start of the game
128 hapticEffectDuration = HAR.GetVibrationLength(HapticMaterialId);
129 currentSourceIntensity = sourceIntensity;
130 if (hapticMaterial!=null)
131 {
132 DebugMode("Haptic effect duration: " + hapticEffectDuration + " " + hapticMaterial.name);
133 }
134 else
135 {
136 DebugMode("Parametric Haptic effect duration: " + hapticEffectDuration + " " + HapticMaterialId);
137 }
138 if (isLooping)
139 {
140 float maxComputedTimeInLoops = maxLoops * (float)hapticEffectDuration;
141 if (maxComputedTimeInLoops < maxLoopTime)
142 {
143 maxLoopTime = maxComputedTimeInLoops;
144 }
145 else
146 {
147 maxLoops = (int)(maxLoopTime / (float)hapticEffectDuration);
149 }
150 }
151 else
152 {
154 maxLoops = 1;
155 }
156 if (playAtStart)
157 {
159 }
160 }
virtual void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...

References Interhaptics.Internal.HapticSource.DebugMode(), Interhaptics.Internal.HapticSource.hapticEffectDuration, Interhaptics.Internal.HapticSource.hapticMaterial, Interhaptics.Internal.HapticSource.HapticMaterialId, Interhaptics.Internal.HapticSource.isLooping, Interhaptics.Internal.HapticSource.maxLoops, Interhaptics.Internal.HapticSource.maxLoopTime, Interhaptics.Internal.HapticSource.playAtStart, and Interhaptics.Internal.HapticSource.PlayEventVibration().

◆ Stop()

virtual void Interhaptics.Internal.HapticSource.Stop ( )
virtual

Call this method to stop the haptic effect.

Reimplemented in Interhaptics.Utils.AudioHapticSource, Interhaptics.Utils.EventHapticSource, and Interhaptics.Utils.ParametricHapticSource.

Definition at line 222 of file HapticSource.cs.

223 {
224 isPlaying = false;
225 Core.HAR.StopEvent(HapticMaterialId);
226#if (UNITY_ANDROID && !ENABLE_METAQUEST) || UNITY_IOS && !UNITY_EDITOR
227 HAR.StopAllEvents();
228#endif
229 if (playingCoroutine != null)
230 {
231 StopCoroutine(playingCoroutine);
232 playingCoroutine = null;
233 }
234 }

References Interhaptics.Internal.HapticSource.HapticMaterialId, Interhaptics.Internal.HapticSource.isPlaying, and Interhaptics.Internal.HapticSource.playingCoroutine.

◆ Update()

virtual void Interhaptics.Internal.HapticSource.Update ( )
protectedvirtual

Update the haptic effect settings at every frame.

Definition at line 165 of file HapticSource.cs.

166 {
167 if (sourceIntensity != currentSourceIntensity)
168 {
170 currentSourceIntensity = sourceIntensity;
171 DebugMode("Source intensity changed to: " + sourceIntensity);
172 }
173 }
void ApplySourceIntensity()
Call this method to apply the source intensity.

References Interhaptics.Internal.HapticSource.ApplySourceIntensity(), and Interhaptics.Internal.HapticSource.DebugMode().

Member Data Documentation

◆ debugMode

bool Interhaptics.Internal.HapticSource.debugMode

Definition at line 38 of file HapticSource.cs.

Referenced by Interhaptics.Internal.HapticSource.DebugMode().

◆ ERROR_MESSAGE_MONO

const string Interhaptics.Internal.HapticSource.ERROR_MESSAGE_MONO = "Interhaptics requires IL2CPP scripting backend for Android. Please change it in Player Settings. Haptics will not play on the Mono scripting backend on the Android platform."
static

◆ hapticEffectDuration

◆ hapticEffectStreamingAssetsPath

string Interhaptics.Internal.HapticSource.hapticEffectStreamingAssetsPath

Definition at line 27 of file HapticSource.cs.

Referenced by Interhaptics.Internal.HapticSource.Awake().

◆ hapticMaterial

HapticMaterial Interhaptics.Internal.HapticSource.hapticMaterial

◆ isLooping

◆ isPlaying

◆ loopStartTime

double Interhaptics.Internal.HapticSource.loopStartTime = 0

◆ maxLoops

◆ maxLoopTime

float Interhaptics.Internal.HapticSource.maxLoopTime = 10f

◆ playAtStart

◆ playingCoroutine

◆ targetIntensity

double Interhaptics.Internal.HapticSource.targetIntensity = 1.0

Definition at line 51 of file HapticSource.cs.

◆ useStreamingAssets

bool Interhaptics.Internal.HapticSource.useStreamingAssets = false

Definition at line 24 of file HapticSource.cs.

Referenced by Interhaptics.Internal.HapticSource.Awake().

◆ vibrationOffset

float Interhaptics.Internal.HapticSource.vibrationOffset

Property Documentation

◆ HapticMaterialId

◆ SourceIntensity

float Interhaptics.Internal.HapticSource.SourceIntensity
getset

Definition at line 71 of file HapticSource.cs.

72 {
73 get => sourceIntensity;
74 set
75 {
76 sourceIntensity = value;
77 DebugMode("Source intensity set to: " + sourceIntensity);
78 ApplySourceIntensity(); // Apply the intensity immediately when set
79 }
80 }

The documentation for this class was generated from the following file: