Tanoda
unity-ui-extensions/Runtime/Scripts/Controls/ColorPicker/ColorLabel.cs
Go to the documentation of this file.
1
3
4
6{
7
8 [RequireComponent(typeof(Text))]
9public class ColorLabel : MonoBehaviour
10{
12
14
15 public string prefix = "R: ";
16 public float minValue = 0;
17 public float maxValue = 255;
18
19 public int precision = 0;
20
21 private Text label;
22
23 private void Awake()
24 {
25 label = GetComponent<Text>();
26
27 }
28
29 private void OnEnable()
30 {
31 if (Application.isPlaying && picker != null)
32 {
33 picker.onValueChanged.AddListener(ColorChanged);
34 picker.onHSVChanged.AddListener(HSVChanged);
35 }
36 }
37
38 private void OnDestroy()
39 {
40 if (picker != null)
41 {
42 picker.onValueChanged.RemoveListener(ColorChanged);
43 picker.onHSVChanged.RemoveListener(HSVChanged);
44 }
45 }
46
47#if UNITY_EDITOR
48 private void OnValidate()
49 {
50 label = GetComponent<Text>();
51 UpdateValue();
52 }
53#endif
54
55 private void ColorChanged(Color color)
56 {
57 UpdateValue();
58 }
59
60 private void HSVChanged(float hue, float sateration, float value)
61 {
62 UpdateValue();
63 }
64
65 private void UpdateValue()
66 {
67 if (picker == null)
68 {
69 label.text = prefix + "-";
70 }
71 else
72 {
73 float value = minValue + (picker.GetValue(type) * (maxValue - minValue));
74
75 label.text = prefix + ConvertToDisplayString(value);
76 }
77 }
78
79 private string ConvertToDisplayString(float value)
80 {
81 if (precision > 0)
82 return value.ToString("f " + precision);
83 else
84 return Mathf.FloorToInt(value).ToString();
85 }
86}
87}
UnityEngine.Color Color
Definition: TestScript.cs:32