4[RequireComponent(typeof(InputField))]
11 private InputField hexInputField;
15 hexInputField = GetComponent<InputField>();
18 hexInputField.onEndEdit.AddListener(UpdateColor);
22 private void OnDestroy()
24 hexInputField.onValueChanged.RemoveListener(UpdateColor);
28 private void UpdateHex(
Color newColor)
30 hexInputField.text = ColorToHex(newColor);
33 private void UpdateColor(
string newHex)
36 if (!newHex.StartsWith(
"#"))
37 newHex =
"#" + newHex;
38 if (ColorUtility.TryParseHtmlString(newHex, out color))
42 "hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
45 private string ColorToHex(Color32 color)
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);
ColorChangedEvent onValueChanged