Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
GameInputXInputHapticController.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using System;
7using UnityEngine;
10
12{
13 public class GameInputXInputHapticController : MonoBehaviour
14 {
15 [SerializeField]
16 [Tooltip("Add EventHapticSources for vibrations on button press or event calls")]
17 private EventHapticSource[] eventHapticSources;
18 [SerializeField]
19 [Tooltip("Add AudioHapticSources for vibrations on button press or event calls")]
20 private AudioHapticSource[] audioHapticSources;
21 [SerializeField]
22 [Tooltip("Add SpatialHapticSources for vibrations on button press or collision/trigger calls")]
23 private SpatialHapticSource[] spatialHapticSources;
24 [SerializeField]
25 [Tooltip("Add EventHapticSources for vibrations on index (GameInput exclusive feature)")]
26 private EventHapticSource[] triggerEventHapticSources;
27 [SerializeField]
28 private TextMesh buttonPressed;
29 [SerializeField]
30 private TextMesh vibrationMaterialName;
31 [SerializeField]
32 private TextMesh vibrationType;
33 [SerializeField]
34 private int m_StickId = 1;
35 [SerializeField]
36 [Range(0, 6)]
37 private int indexStiffness;
38 [SerializeField]
39 [Range(0, 2)]
40 private int indexVibration;
41 [SerializeField]
42 public bool debugMode;
43 private bool canModifyVibrationType = true;
44 private AudioSource[] allAudioSources;
45 private SpatialHapticSource[] allSpatialHapticSources;
46
47 private void Start()
48 {
49 VibrationHapticSourceGUI();
50 }
51
56 public void DebugMode(string debugMessage)
57 {
58 if (debugMode)
59 {
60 Debug.Log(debugMessage);
61 }
62 }
63
64 private void ResetHapticSources()
65 {
66 Core.HAR.StopAllEvents();
67 foreach (EventHapticSource hapticSource in eventHapticSources)
68 {
69 hapticSource.Stop(); // Ensure the haptic effect is stopped.
70 if (hapticSource.playingCoroutine != null)
71 {
72 StopCoroutine(hapticSource.playingCoroutine); // Stop the coroutine if it's running.
73 hapticSource.playingCoroutine = null; // Clear the reference.
74 }
75 hapticSource.isPlaying = false; // Reset the isPlaying state.
76 }
77 allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
78 foreach (AudioSource audioSource in allAudioSources)
79 {
80 if (audioSource.isPlaying)
81 {
82 audioSource.Stop();
83 Debug.Log("Audio Source stopped: " + audioSource);
84 break;
85 }
86 }
87 allSpatialHapticSources = FindObjectsOfType(typeof(SpatialHapticSource)) as SpatialHapticSource[];
88 foreach (SpatialHapticSource spatialHapticSource in allSpatialHapticSources)
89 {
90 if (spatialHapticSource.GetComponent<ObjectTransform>().buttonPressed)
91 {
92 spatialHapticSource.GetComponent<ObjectTransform>().buttonPressed = false;
93 spatialHapticSource.GetComponent<ObjectTransform>().ResetPosition();
94 break;
95 }
96 }
97 }
98
99 private void OnApplicationFocus(bool hasFocus)
100 {
101 if ((!hasFocus) && (HapticManager.StopHapticsOnFocusLoss))
102 {
103 ResetHapticSources();
104 }
105 }
106
107 private void OnApplicationPause(bool pauseStatus)
108 {
109 if (pauseStatus && HapticManager.StopHapticsOnFocusLoss)
110 {
111 ResetHapticSources();
112 }
113
114 }
115 private void Update()
116 {
117 VibrationButtons();
118 GameInputTriggers();
119 ButtonsUI();
120 }
121
122 private void GameInputTriggers()
123 {
124 KeyCode[] joystickButtonCodes = {
125 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button4", true),
126 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button5", true)
127 };
128 for (int i = 0; i < joystickButtonCodes.Length; i++)
129 {
130 if (Input.GetKeyDown(joystickButtonCodes[i]))
131 {
132 Debug.Log("Trigger " + i + " pressed");
133 triggerEventHapticSources[i].PlayEventVibration();
134 vibrationMaterialName.text = triggerEventHapticSources[i].name;
135 }
136 }
137 }
138
139 private void VibrationButtons()
140 {
141 float dpadVertical = Input.GetAxis("dpad" + m_StickId + "_vertical");
142 if (dpadVertical == 0)
143 {
144 canModifyVibrationType = true;
145 }
146 else
147 {
148 IncreaseIndexVibrations(dpadVertical > 0);
149 }
150 KeyCode[] joystickButtonCodes = {
151 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button0", true),
152 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button1", true),
153 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button2", true),
154 (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button3", true)
155 };
156 for (int i = 0; i < joystickButtonCodes.Length; i++)
157 {
158 if (Input.GetKeyDown(joystickButtonCodes[i]))
159 {
160 HapticVibrationController(indexVibration, i);
161 }
162 }
163 }
164
165 private void HapticVibrationController(int indexVibration, int indexButton)
166 {
167 switch (indexVibration)
168 {
169 case 0:
170 PlayAudioVibrationController(indexButton);
171 break;
172 case 1:
173 PlayEventVibrationController(indexButton);
174 break;
175 case 2:
177 break;
178 default:
179 Debug.LogWarning("Invalid input for indexVibration.");
180 break;
181 }
182 }
183
184 public void PlayAudioVibrationController(int indexButton)
185 {
186 ResetHapticSources();
187 audioHapticSources[indexButton].PlayEventVibration();
188 vibrationMaterialName.text = audioHapticSources[indexButton].name;
189 }
190
191 public void PlayEventVibrationController(int indexButton)
192 {
193 ResetHapticSources();
194 eventHapticSources[indexButton].PlayEventVibration();
195 vibrationMaterialName.text = eventHapticSources[indexButton].name;
196 }
197
198 public void PlaySpatialVibrationController(int indexButton)
199 {
200 ResetHapticSources();
201 spatialHapticSources[indexButton].GetComponent<ObjectTransform>().enabled = true;
202 spatialHapticSources[indexButton].GetComponent<ObjectTransform>().buttonPressed = true;
203 vibrationMaterialName.text = spatialHapticSources[indexButton].name;
204 }
205
206 public void IncreaseIndexVibrations(bool isIncrementing)
207 {
208 if (canModifyVibrationType)
209 {
210 indexVibration = isIncrementing ? (indexVibration + 1) % 3 : (indexVibration + 2) % 3;
211 canModifyVibrationType = false;
212 }
213 VibrationHapticSourceGUI();
214 }
215
216 private void VibrationHapticSourceGUI()
217 {
218 switch (indexVibration)
219 {
220 case 0:
221 vibrationType.text = "Audio Haptic Source";
222 break;
223 case 1:
224 vibrationType.text = "Event Haptic Source";
225 break;
226 case 2:
227 vibrationType.text = "Spatial Haptic Source";
228 break;
229 default:
230 Debug.LogWarning("Invalid input for Haptic Source.");
231 break;
232 }
233 }
234
235 private void ButtonsUI()
236 {
237 string[] buttonNames = { "A | Cross", "B | Circle", "X | Square", "Y | Triangle", "Shoulder Left", "Shoulder Right" };
238 string[] dpadNames = { "DPad left", "DPad right", "DPad down", "DPad up" };
239 for (int i = 0; i < buttonNames.Length; i++)
240 {
241 if (Input.GetKey((KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + m_StickId + "Button" + i, true)))
242 {
243 buttonPressed.text = buttonNames[i];
244 return;
245 }
246 }
247 for (int i = 0; i < dpadNames.Length; i++)
248 {
249 if (i < 2 && Input.GetAxis("dpad" + m_StickId + "_horizontal") == (i == 0 ? -1 : 1))
250 {
251 buttonPressed.text = dpadNames[i] + " " + Input.GetAxis("dpad" + m_StickId + "_horizontal");
252 return;
253 }
254 else if (i >= 2 && Input.GetAxis("dpad" + m_StickId + "_vertical") == (i == 2 ? -1 : 1))
255 {
256 buttonPressed.text = dpadNames[i] + " " + Input.GetAxis("dpad" + m_StickId + "_vertical");
257 return;
258 }
259 }
260 }
261 }
262}
263
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...
Haptic source that plays a haptic effect when triggered by an event. Plays the haptic effect on the s...
override void Stop()
Stops the haptic effect on the specified body parts.
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...