-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlider.cs
More file actions
40 lines (34 loc) · 1.1 KB
/
Slider.cs
File metadata and controls
40 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using UnityEngine;
using UnityEngine.UI;
public class Slider : MonoBehaviour
{
public float min = 0;
public float max = 100;
public float current = 100;
public Transform sliderBar;
public Text textField;
public string unit = "m/s";
public Color defaultColor = Color.white;
public Color overShootColor = Color.yellow;
// Use this for initialization
public void SetMin(float _min) { min = _min; }
public void SetMax(float _max) { max = _max; }
public void SetValue(float val)
{
current = val > min ? val : min; // lower Clamp
UpdateSlider();
}
private void UpdateSlider()
{
// set slider
Vector3 scale = sliderBar.localScale;
scale.x = current / (max-min) >0 ? current / (max-min):0;
sliderBar.localScale = scale;
if (current < max)
sliderBar.GetComponent<Image>().color = defaultColor;
else
sliderBar.GetComponent<Image>().color = overShootColor;
// set text
if(textField!=null) textField.text = ""+current.ToString("F0") + " "+ unit;
}
}