0

I am setting my textmeshpro's text color with RGBA values. I would like to set it instead with hex code. I have an array of hex color values. How do I set this in Unity?

using UnityEngine.UI;

public TMP_Text textMeshPro;
public string[] colorListHex = { "#FF0000", "#754C24", "#5DA500"};

void Start(){
   textMeshPro.color = new Color32 (255, 0, 0, 255); //How do I set it to be colorListHex[0] instead of using rgba?
}
Mitch
  • 41
  • 5
  • Does this answer your question? [How do I get the color from a hexadecimal color code using .NET?](https://stackoverflow.com/questions/2109756/how-do-i-get-the-color-from-a-hexadecimal-color-code-using-net) – Connor Stoop Jun 04 '21 at 09:19
  • @ConnorStoop hi sorry, but from Unity's side, i think it does not work. – Mitch Jun 04 '21 at 09:23
  • check [this](https://docs.unity3d.com/ScriptReference/ColorUtility.ToHtmlStringRGB.html). `ColorUtility.ToHtmlStringRGB` returns a string Hexadecimal string representing the color. – rustyBucketBay Jun 04 '21 at 10:04

1 Answers1

1

Try this...

Color colorFromHex;
ColorUtility.TryParseHtmlString(colorListHex[0], out colorFromHex);

https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html

Mr T
  • 26
  • 3
  • Can be done in a single line `ColorUtility.TryParseHtmlString(colorListHex[0], out var colorFromHex);` ;) and should always be used with `if` to check if it was actually able to parse or not – derHugo Jun 04 '21 at 20:44