Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
SliderArrayController.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3using System.Collections;
4
6{
7 public class SliderArrayController : MonoBehaviour
8 {
9 public Slider[] sliders; // Array of Sliders
10 private int currentSliderIndex = 0; // Index of the currently selected slider
11 public Color activeColor = Color.red; // Color for the active slider
12 public Color activeKnobColor = new Color(0, 0, 0, 0.5f);
13 public Color inactiveColor = Color.white; // Color for inactive sliders
14 public Color inactiveKnobColor = Color.white;
15 private bool isTransitioning = false; // Flag to check if a transition is already happening
16 public float transitionDelay = 0.5f; // Delay for transitions
17
18 void Start()
19 {
20 // Initialize all sliders to the inactive color
21 foreach (var slider in sliders)
22 {
23 SetSliderColor(slider, inactiveColor);
24 SetKnobColor(slider, inactiveKnobColor);
25 }
26 // Highlight the first slider
27 SetSliderColor(sliders[currentSliderIndex], activeColor);
28 SetKnobColor(sliders[currentSliderIndex], activeKnobColor);
29 }
30
31 void Update()
32 {
33 // Cycle through sliders with the right stick vertical input
34 if (!isTransitioning && Input.GetAxis("rightstick1vertical") > 0)
35 {
36 StartCoroutine(TransitionToNextSlider());
37 }
38 else if (!isTransitioning && Input.GetAxis("rightstick1vertical") < 0)
39 {
40 StartCoroutine(TransitionToPreviousSlider());
41 }
42
43 // Control the current slider with the left stick vertical input
44 if (sliders[currentSliderIndex] != null)
45 {
46 float verticalInput = Input.GetAxis("leftstick1vertical");
47 sliders[currentSliderIndex].value += verticalInput * Time.deltaTime;
48 }
49 }
50
51 private IEnumerator TransitionToNextSlider()
52 {
53 isTransitioning = true;
54
55 // Change previous slider to inactive color
56 SetSliderColor(sliders[currentSliderIndex], inactiveColor);
57 SetKnobColor(sliders[currentSliderIndex], inactiveKnobColor);
58
59 // Move to the next slider
60 currentSliderIndex = (currentSliderIndex + 1) % sliders.Length;
61
62 yield return new WaitForSeconds(transitionDelay);
63
64 // Change new active slider to active color
65 SetSliderColor(sliders[currentSliderIndex], activeColor);
66 SetKnobColor(sliders[currentSliderIndex], activeKnobColor);
67
68 isTransitioning = false;
69 }
70
71 private IEnumerator TransitionToPreviousSlider()
72 {
73 isTransitioning = true;
74
75 // Change previous slider to inactive color
76 SetSliderColor(sliders[currentSliderIndex], inactiveColor);
77 SetKnobColor(sliders[currentSliderIndex], inactiveKnobColor);
78
79 // Move to the previous slider
80 currentSliderIndex = (currentSliderIndex - 1 + sliders.Length) % sliders.Length;
81
82 yield return new WaitForSeconds(transitionDelay);
83
84 // Change new active slider to active color
85 SetSliderColor(sliders[currentSliderIndex], activeColor);
86 SetKnobColor(sliders[currentSliderIndex], activeKnobColor);
87
88 isTransitioning = false;
89 }
90
91 private void SetSliderColor(Slider slider, Color color)
92 {
93 // Assuming the slider has an Image component
94 var sliderImage = slider.GetComponentInChildren<Image>();
95 if (sliderImage != null)
96 {
97 sliderImage.color = color;
98 }
99 }
100
101 private void SetKnobColor(Slider slider, Color color)
102 {
103 slider.handleRect.GetComponent<Image>().color = color;
104 }
105 }
106}