Tanoda
HSVPicker/UI/HexColorField.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3
4[RequireComponent(typeof(InputField))]
5public class HexColorField : MonoBehaviour
6{
8
9 public bool displayAlpha;
10
11 private InputField hexInputField;
12
13 private void Awake()
14 {
15 hexInputField = GetComponent<InputField>();
16
17 // Add listeners to keep text (and color) up to date
18 hexInputField.onEndEdit.AddListener(UpdateColor);
19 hsvpicker.onValueChanged.AddListener(UpdateHex);
20 }
21
22 private void OnDestroy()
23 {
24 hexInputField.onValueChanged.RemoveListener(UpdateColor);
25 hsvpicker.onValueChanged.RemoveListener(UpdateHex);
26 }
27
28 private void UpdateHex(Color newColor)
29 {
30 hexInputField.text = ColorToHex(newColor);
31 }
32
33 private void UpdateColor(string newHex)
34 {
35 Color color;
36 if (!newHex.StartsWith("#"))
37 newHex = "#" + newHex;
38 if (ColorUtility.TryParseHtmlString(newHex, out color))
39 hsvpicker.CurrentColor = color;
40 else
41 Debug.Log(
42 "hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
43 }
44
45 private string ColorToHex(Color32 color)
46 {
47 return displayAlpha
48 ? string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a)
49 : string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
50 }
51}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
ColorChangedEvent onValueChanged
Definition: ColorPicker.cs:19
Color CurrentColor
Definition: ColorPicker.cs:23