Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
HapticEffectCodeTester.cs
Go to the documentation of this file.
1using UnityEngine;
7
9{
13 public class HapticEffectCodeTester : MonoBehaviour
14 {
15 public HapticMaterial hapticMaterial; // Assign this in the Unity Inspector
16 public double intensity = 1.0; // Intensity of the haptic effect
17 double[] transientTester = { 0.02, 1.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0 }; // Transient pattern
18 // Define your amplitude, pitch, and transient values
19 double[] amplitude = { 0.0, 1.0, 2.5, 1.0 }; // Constant amplitude
20 double[] pitch = { 0.0, 0.0, 1.5, 1.0, 2.5, 0.0 }; // Pitch pattern
21 double[] transient = { 3.0, 1.0, 1.0, 4.0, 1.0, 1.0 , 4.5, 1.0, 1.0}; // Transient pattern
22
23 void Update()
24 {
25 // Trigger the haptic effect based on a condition, for example, a button press
26 if (Input.GetKeyDown(KeyCode.K))
27 {
29 }
30
31 if (Input.GetKeyDown(KeyCode.I))
32 {
34 }
35
36 if (Input.GetKeyDown(KeyCode.U))
37 {
39 }
40
41 if (Input.GetKeyDown(KeyCode.J))
42 {
44 }
45
46 if (Input.GetKeyDown(KeyCode.H))
47 {
49 }
50
51 if (Input.GetKeyDown(KeyCode.M))
52 {
54 }
55
56 if (Input.GetKeyDown(KeyCode.L))
57 {
58 // Play the haptic effect
59 HAR.PlayHapticEffect(hapticMaterial, 0.5, 2, 0f, LateralFlag.Global);
60 //StartCoroutine(HAR.PlayHapticEffectCoroutine(hapticMaterial, 1.0, LateralFlag.Left, true, 3));
61 }
62 if (Input.GetKeyDown(KeyCode.O))
63 {
65 }
66 if (Input.GetKeyDown(KeyCode.P))
67 {
69 }
70
71 if (Input.GetKeyDown(KeyCode.Alpha0))
72 {
74 }
75 // Detect key presses for numbers 1 to 9 and play corresponding haptic effect
76 for (int i = 1; i <= 9; i++)
77 {
78 if (Input.GetKeyDown(KeyCode.Alpha0 + i))
79 {
80 HapticPreset.Play((HapticPreset.PresetType)(i - 1));
81 }
82 }
83 }
84
85 public void CodeTester()
86 {
87 HapticMaterial myHapticMaterial = null;
88 if (myHapticMaterial != null)
89 {
90 HAR.PlayHapticEffect(myHapticMaterial);
91 Debug.Log("Haptic effect played successfully!");
92 }
93 else
94 {
95 Debug.LogError("Haptic material is not assigned!");
96 }
97 }
98 public void TestConstant()
99 {
100 HAR.PlayConstant(0.5, 0.5); // Plays a constant haptic effect with 50% amplitude for 0.5s.
101 }
102
103 public void TestConstantLoop()
104 {
105 double[] amplitude = { 0.0, 0.5, 0.5, 0.5 };
106 //HAR.PlayParametricHapticEffectWithLoop(amplitude, null, 65, 300, null, 1.0, 2, LateralFlag.Global);
107 HAR.PlayConstant(0.5, 0.5, 1, 2, LateralFlag.Global);
108 }
109
110 public void TestConstantRight()
111 {
112 HAR.PlayConstant(1.0, 5.0, _controllerSide: LateralFlag.Right);
113 }
115 {
116 HAR.PlayAdvanced(amplitude, pitch, HAR.DEFAULT_FREQ_MIN, HAR.DEFAULT_FREQ_MAX, transient, 1.0, 2); // Intensity set to 1.0
117 }
118 public void TestHapticEffect()
119 {
120 HAR.PlayHapticEffect(hapticMaterial, 1.0, 1, 0f, LateralFlag.Global);
121 }
122 public void TestTransient()
123 {
124 // Set the time, amplitude, and pitch for the transient
125 double time = 0.0; // Half a second after being called
126 double amplitude = 1.0; // strong transient
127 double pitch = 1.0; // High pitch
128
129 // Optional parameters
130 double intensity = 1.0; // Full intensity
131 int loops = 1; // No looping
132 LateralFlag controllerSide = LateralFlag.Global; // Play on both sides
133
134 // Play the transient haptic effect
135 HAR.PlayTransient(time, amplitude, pitch, intensity, loops, controllerSide);
136 //HAR.PlayTransient(time, amplitude, pitch); same effect with default parameters
137 //HAR.PlayTransient(); // Plays a transient immediately with the default values 1.0 and 1.0 for amplitude and pitch
138 }
139 public void TestTransients()
140 {
141 // Transients expressed as time amplitude pitch triplets
142 double[] transient = {
143 0.0, 1.0, 0.5,
144 0.25, 0.75, 0.5,
145 0.5, 0.5, 0.5,
146 0.75, 0.25, 0.5
147 };
148 HAR.PlayTransients(transient);
149 }
150
151 public void TestTransientsLoop()
152 {
153 // Transients expressed as time amplitude pitch triplets
154 double[] transient = {
155 0.0, 1.0, 0.5,
156 0.25, 0.75, 0.5,
157 0.5, 0.5, 0.5,
158 0.75, 0.25, 0.5
159 };
160 HAR.PlayTransients(transient, 1.0, 2, LateralFlag.Global);
161 //HAR.PlayParametricHapticEffectWithLoop(null, null, 65, 300, transient, 1.0, 2, LateralFlag.Global);
162 }
163 public void TestAmplitude()
164 {
165 // Define an array of time-amplitude pairs
166 HAR.Play(new double[] { 0, 1, 1, 0.0, 2, 1 }); // plays a vibrations starting at maximum amplitude, decrease at 0.5 at 1 second, and back to 1 at 2 seconds
167 }
168
169 public void TestAmplitudesLoop()
170 {
171 double[] amplitude = { 0, 1, 1, 0.0, 2, 1 };
172 // HAR.PlayParametricHapticEffectWithLoop(amplitude, null, 65, 300, null, 1.0, 2, LateralFlag.Global);
173 HAR.Play(amplitude, 1.0, 2, LateralFlag.Global);
174 }
175
177 {
178 // Amplitude array as time amplitude pairs
179 double[] amplitude = {
180 0.0, 0.5,
181 2.0, 0.5,
182 };
183 // Transients expressed as time amplitude pitch triplets
184 double[] transient = {
185 0.5, 1, 0.5,
186 1.5, 0.75, 0.5
187 };
188 HAR.Play(amplitude, transient, 1.0, 1, LateralFlag.Global); //Plays the complex pattern described by the arrays 2 times at intensity one on the left side. See Intensity Controls
189 }
190
192 {
193 // Amplitude array as time amplitude pairs
194 double[] amplitude = {
195 0.0, 0.5,
196 2.0, 0.5,
197 };
198 // Transients expressed as time amplitude pitch triplets
199 double[] transient = {
200 0.5, 1, 0.5,
201 1.5, 0.75, 0.5
202 };
203 HAR.Play(amplitude, transient, 1.0, 2, LateralFlag.Global);
204 }
205
206 public void TestFrequency()
207 {
208 // Amplitude at 0.5 between 0 and 2 seconds
209 double[] amplitudes = {
210 1.0, 0.5,
211 3.0, 0.5
212 };
213 // pitch between 0 and 1
214 double[] pitch = {
215 1.0, 0.0,
216 3.0, 1.0
217 };
218 HAR.PlayAdvanced(amplitudes, pitch);
219 }
220
221 public void TestFrequencyLoop()
222 {
223 // Amplitude at 0.5 between 0 and 2 seconds
224 double[] amplitudes = {
225 1.0, 0.5,
226 3.0, 0.5
227 };
228 // pitch between 0 and 1
229 double[] pitch = {
230 1.0, 0.0,
231 3.0, 1.0
232 };
233 HAR.PlayAdvanced(amplitudes, pitch, 65, 300, null, 1.0, 2, LateralFlag.Global);
234 }
235
237 {
238 // Amplitude array
239 double[] amplitudes = {
240 0.0, 0.0,
241 1.0, 1.0,
242 2.0, 0.5,
243 3.0, 0.25,
244 4.0, 0.25
245 };
246 // pitch array
247 double[] pitch = {
248 0.0, 1,
249 2.0, 0,
250 4.0, 1
251 };
252 // transient array
253 double[] transients = {
254 0.5, 1, 0.5,
255 3.5, 0.5, 0.5
256 };
257 double _fmin = 65;
258 double _fmax = 300;
259 HAR.PlayAdvanced(amplitudes, pitch, _fmin, _fmax, transients);
260 }
261
263 {
264 // Amplitude array
265 double[] amplitudes = {
266 0.0, 0.0,
267 1.0, 1.0,
268 2.0, 0.5,
269 3.0, 0.25,
270 4.0, 0.25
271 };
272 // pitch array
273 double[] pitch = {
274 0.0, 1,
275 2.0, 0,
276 4.0, 1
277 };
278 // transient array
279 double[] transients = {
280 0.5, 1, 0.5,
281 3.5, 0.5, 0.5
282 };
283 double _fmin = 65;
284 double _fmax = 300;
285 HAR.PlayAdvanced(amplitudes, pitch, _fmin, _fmax, transients, 1.0, 2, LateralFlag.Global);
286 //HAR.PlayParametricHapticEffectWithLoop(amplitudes, pitch, _fmin, _fmax, transients, 1.0, 2, LateralFlag.Global);
287 }
288
289 public void TestLongHaptics()
290 {
291 // Amplitude array
292 double[] amplitudes = {
293 0.0, 0.0,
294 4.0, 1.0
295 };
296 // pitch array
297 double[] pitch = {
298 0.0, 0,
299 4.0, 1
300 };
301 double _fmin = 65;
302 double _fmax = 300;
303 HAR.PlayAdvanced(amplitudes, pitch, _fmin, _fmax, null, 1.0, 1, LateralFlag.Global);
304 }
305
306 public void PlayStockHaptics(int i)
307 {
308 HapticPreset.Play((HapticPreset.PresetType)(i));
309 }
310 public void StopMobileHaptics()
311 {
312#if UNITY_ANDROID && !UNITY_EDITOR
314#endif
315#if UNITY_IOS
317#endif
319 HAR.MobileCancelHaptics();
320 }
321 public void StopHapticEffect()
322 {
323 HAR.StopAllEvents();
324 }
325 }
326
327}
Represents a haptic material, which is a ScriptableObject in Unity. This class is used to handle hapt...
Class to test the different haptic effects in the Interhaptics SDK triggered by key presses on the co...
LateralFlag
Enumeration for lateral flag in haptic command data.