Tanoda
unity-ui-extensions/Runtime/Scripts/Controls/ColorPicker/ColorSliderImage.cs
Go to the documentation of this file.
1
3
5{
6 [RequireComponent(typeof(RawImage)), ExecuteInEditMode()]
7 public class ColorSliderImage : MonoBehaviour
8 {
10
15
16 public Slider.Direction direction;
17
18 private RawImage image;
19
20 private RectTransform RectTransform
21 {
22 get
23 {
24 return transform as RectTransform;
25 }
26 }
27
28 private void Awake()
29 {
30 image = GetComponent<RawImage>();
31 if (image)
32 {
33 RegenerateTexture();
34 }
35 else
36 {
37 Debug.LogWarning("Missing RawImage on object [" + name + "]");
38 }
39 }
40
41 private void OnEnable()
42 {
43 if (picker != null && Application.isPlaying)
44 {
45 picker.onValueChanged.AddListener(ColorChanged);
46 picker.onHSVChanged.AddListener(ColorChanged);
47 }
48 }
49
50 private void OnDisable()
51 {
52 if (picker != null)
53 {
54 picker.onValueChanged.RemoveListener(ColorChanged);
55 picker.onHSVChanged.RemoveListener(ColorChanged);
56 }
57 }
58
59 private void OnDestroy()
60 {
61 if (image.texture != null)
62 DestroyImmediate(image.texture);
63 }
64
65#if UNITY_EDITOR
66 private void OnValidate()
67 {
68 image = GetComponent<RawImage>();
69 if (image)
70 {
71 RegenerateTexture();
72 }
73 else
74 {
75 Debug.LogWarning("Missing RawImage on object [" + name + "]");
76 }
77 }
78#endif
79
80 private void ColorChanged(Color newColor)
81 {
82 switch (type)
83 {
84 case ColorValues.R:
85 case ColorValues.G:
86 case ColorValues.B:
87 case ColorValues.Saturation:
88 case ColorValues.Value:
89 RegenerateTexture();
90 break;
91 case ColorValues.A:
92 case ColorValues.Hue:
93 default:
94 break;
95 }
96 }
97
98 private void ColorChanged(float hue, float saturation, float value)
99 {
100 switch (type)
101 {
102 case ColorValues.R:
103 case ColorValues.G:
104 case ColorValues.B:
105 case ColorValues.Saturation:
106 case ColorValues.Value:
107 RegenerateTexture();
108 break;
109 case ColorValues.A:
110 case ColorValues.Hue:
111 default:
112 break;
113 }
114 }
115
116 private void RegenerateTexture()
117 {
118 if (!picker)
119 {
120 Debug.LogWarning("Missing Picker on object [" + name + "]");
121 }
122 Color32 baseColor = picker != null ? picker.CurrentColor : Color.black;
123
124 float h = picker != null ? picker.H : 0;
125 float s = picker != null ? picker.S : 0;
126 float v = picker != null ? picker.V : 0;
127
128 Texture2D texture;
129 Color32[] colors;
130
131 bool vertical = direction == Slider.Direction.BottomToTop || direction == Slider.Direction.TopToBottom;
132 bool inverted = direction == Slider.Direction.TopToBottom || direction == Slider.Direction.RightToLeft;
133
134 int size;
135 switch (type)
136 {
137 case ColorValues.R:
138 case ColorValues.G:
139 case ColorValues.B:
140 case ColorValues.A:
141 size = 255;
142 break;
143 case ColorValues.Hue:
144 size = 360;
145 break;
146 case ColorValues.Saturation:
147 case ColorValues.Value:
148 size = 100;
149 break;
150 default:
151 throw new System.NotImplementedException("");
152 }
153 if (vertical)
154 texture = new Texture2D(1, size);
155 else
156 texture = new Texture2D(size, 1);
157
158 texture.hideFlags = HideFlags.DontSave;
159 colors = new Color32[size];
160
161 switch (type)
162 {
163 case ColorValues.R:
164 for (byte i = 0; i < size; i++)
165 {
166 colors[inverted ? size - 1 - i : i] = new Color32(i, baseColor.g, baseColor.b, 255);
167 }
168 break;
169 case ColorValues.G:
170 for (byte i = 0; i < size; i++)
171 {
172 colors[inverted ? size - 1 - i : i] = new Color32(baseColor.r, i, baseColor.b, 255);
173 }
174 break;
175 case ColorValues.B:
176 for (byte i = 0; i < size; i++)
177 {
178 colors[inverted ? size - 1 - i : i] = new Color32(baseColor.r, baseColor.g, i, 255);
179 }
180 break;
181 case ColorValues.A:
182 for (byte i = 0; i < size; i++)
183 {
184 colors[inverted ? size - 1 - i : i] = new Color32(i, i, i, 255);
185 }
186 break;
187 case ColorValues.Hue:
188 for (int i = 0; i < size; i++)
189 {
190 colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(i, 1, 1, 1);
191 }
192 break;
193 case ColorValues.Saturation:
194 for (int i = 0; i < size; i++)
195 {
196 colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(h * 360, (float)i / size, v, 1);
197 }
198 break;
199 case ColorValues.Value:
200 for (int i = 0; i < size; i++)
201 {
202 colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(h * 360, s, (float)i / size, 1);
203 }
204 break;
205 default:
206 throw new System.NotImplementedException("");
207 }
208 texture.SetPixels32(colors);
209 texture.Apply();
210
211 if (image.texture != null)
212 DestroyImmediate(image.texture);
213 image.texture = texture;
214
215 switch (direction)
216 {
217 case Slider.Direction.BottomToTop:
218 case Slider.Direction.TopToBottom:
219 image.uvRect = new Rect(0, 0, 2, 1);
220 break;
221 case Slider.Direction.LeftToRight:
222 case Slider.Direction.RightToLeft:
223 image.uvRect = new Rect(0, 0, 1, 2);
224 break;
225 default:
226 break;
227 }
228 }
229
230 }
231}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32