Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
GlobalHapticIntensityController.cs
Go to the documentation of this file.
2using UnityEngine;
3using UnityEngine.UI;
4
5namespace Interhaptics.Core
6{
7 public class GlobalHapticIntensityController : MonoBehaviour
8 {
9 [Tooltip("The global intensity for all haptic events.")]
10 [Range(0, 1)] // Assuming the intensity range is between 0 and 1
11 public double globalIntensity = 1.0;
12 [Tooltip("Activate Debug Mode for Intensity Controller and for the HapticManager")]
13 [SerializeField]
14 public bool debugMode = false;
15 [SerializeField]
16 private bool stopHapticsOnFocusLoss = true;
17 private double intensityBeforeMute;
18 // Delegate to notify when the intensity changes
19 public delegate void OnIntensityChange(double newIntensity);
21
22 private void Awake()
23 {
24 if (debugMode)
25 {
26 HapticManager.DebugSwitch = true;
27 }
28 if (!stopHapticsOnFocusLoss)
29 {
30 DebugMode("Haptics will not stop on focus loss");
31 HapticManager.StopHapticsOnFocusLoss = stopHapticsOnFocusLoss;
32 }
33 }
34
35 private void Start()
36 {
38 }
39
40 public void StopAllHaptics()
41 {
42 intensityBeforeMute = globalIntensity;
44 }
45
46 public void ResumeAllHaptics()
47 {
48 SetGlobalIntensity(intensityBeforeMute);
49 }
50 private void Update()
51 {
52 if (debugMode)
53 {
54 HapticManager.DebugSwitch = true;
55 }
56 if (globalIntensity!=HAR.GetGlobalIntensity())
57 {
59 }
60 //* Debugging controls
61 if (Input.GetKeyDown(KeyCode.R))
62 {
64 }
65 if (Input.GetKeyDown(KeyCode.Y))
66 {
68 }
69 //*/
70 }
71
72 public void DebugMode(string debugMessage)
73 {
74 if (debugMode)
75 {
76 Debug.Log(debugMessage);
77 }
78 }
79
80 // This method now only sets the global intensity and does not interact with any UI elements
81 public void SetGlobalIntensity(double newIntensity)
82 {
83 globalIntensity = newIntensity;
84 HAR.SetGlobalIntensity(globalIntensity);
85 onIntensityChanged?.Invoke(globalIntensity); // Invoke the event to notify listeners
86 DebugMode($"Global haptic intensity set to: {globalIntensity}");
87 }
88
89 // Call this method to set the global intensity for haptic events
90 public void SetIntensity()
91 {
92 HAR.SetGlobalIntensity(globalIntensity);
93 DebugMode($"Global haptic intensity set to: {globalIntensity}");
94 }
95
96 // Call this method to get the current global intensity for haptic events
97 public double GetCurrentIntensity()
98 {
99 return HAR.GetGlobalIntensity();
100 }
101
102 // Example usage: You might want to call SetIntensity in response to a UI event, like a slider change
103 public void OnIntensitySliderChanged(float newIntensity)
104 {
105 SetGlobalIntensity(newIntensity);
106 }
107
108 // Example usage: You might want to log the current global intensity at certain times
110 {
111 double currentIntensity = GetCurrentIntensity();
112 DebugMode($"Current global haptic intensity is: {currentIntensity}");
113 }
114 }
115}
delegate void OnIntensityChange(double newIntensity)