Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
MobileControl.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2024 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using UnityEngine;
7using System.Collections;
8using System.Collections.Generic;
10
12{
13 public class MobileControl : MonoBehaviour
14 {
15 private static MobileControl currentInstance;
16
17 private struct HapticEffect
18 {
19 public int hMaterialId;
20 public double duration;
21 public int loops;
22 public double intensity;
23 public float vibrationOffset;
24
25 public HapticEffect(int hMaterialId, double duration, int loops, double intensity, float vibrationOffset)
26 {
27 this.hMaterialId = hMaterialId;
28 this.duration = duration;
29 this.loops = loops;
30 this.intensity = intensity;
31 this.vibrationOffset = vibrationOffset;
32 }
33 }
34
35 private Queue<HapticEffect> hapticEffectsQueue = new Queue<HapticEffect>();
36 private bool isPlaying = false;
37 private Coroutine playingCoroutine = null; // Reference to the current playing coroutine
38
39 void Awake()
40 {
41 if (currentInstance != null && currentInstance != this)
42 {
43 Destroy(gameObject);
44 }
45 else
46 {
47 currentInstance = this;
48 DontDestroyOnLoad(gameObject);
49 }
50 }
51
52 public static void EnqueueEffect(int hMaterialId, double duration, int loops, double intensity, float vibrationOffset = 0f)
53 {
54 if (currentInstance == null)
55 {
56 GameObject mobileControllerObject = new GameObject("MobileController");
57 currentInstance = mobileControllerObject.AddComponent<MobileControl>();
58 DontDestroyOnLoad(mobileControllerObject);
59 }
60
61 currentInstance.EnqueueEffectInternal(new HapticEffect(hMaterialId, duration, loops, intensity, vibrationOffset));
62 }
63
64 private void EnqueueEffectInternal(HapticEffect effect)
65 {
66 if (isPlaying)
67 {
68 // Stop the currently playing effect immediately
69 StopCurrentEffect();
70 }
71
72 // Clear the queue and add the new effect
73 hapticEffectsQueue.Clear();
74 hapticEffectsQueue.Enqueue(effect);
75 PlayNextEffect();
76 }
77
78 private void PlayNextEffect()
79 {
80 if (hapticEffectsQueue.Count > 0 && !isPlaying)
81 {
82 var effect = hapticEffectsQueue.Dequeue();
83 playingCoroutine = StartCoroutine(PlayHapticEffect(effect));
84 }
85 }
86
87 private IEnumerator PlayHapticEffect(HapticEffect effect)
88 {
89 if (effect.vibrationOffset > 0)
90 {
91 yield return new WaitForSeconds(effect.vibrationOffset);
92 }
93 isPlaying = true;
94 HAR.StopAllEvents(); // Stop any currently playing haptic events
95
96 HAR.SetEventIntensity(effect.hMaterialId, effect.intensity);
97
98 for (int i = 1; i <= effect.loops; i++)
99 {
100 HAR.PlayEvent(effect.hMaterialId, (double)-Time.realtimeSinceStartup, 0, 0);
101 Debug.Log($"Playing haptic effect: Material ID {effect.hMaterialId}, Loop {i}/{effect.loops}");
102 yield return new WaitForSeconds((float)effect.duration);
103 }
104
105 isPlaying = false;
106 PlayNextEffect(); // Check if there is another effect queued
107 }
108
109 private void StopCurrentEffect()
110 {
111 if (playingCoroutine != null)
112 {
113 StopCoroutine(playingCoroutine);
114 playingCoroutine = null;
115 }
116 HAR.StopAllEvents(); // Ensure to stop the current haptic event
117 isPlaying = false;
118 }
119
120 public static void StopEffects()
121 {
122 if (currentInstance != null)
123 {
124 currentInstance.StopCurrentEffect();
125 currentInstance.hapticEffectsQueue.Clear();
126 Debug.Log("All haptic effects stopped.");
127 }
128 }
129 }
130}
static void EnqueueEffect(int hMaterialId, double duration, int loops, double intensity, float vibrationOffset=0f)