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