Home > Software design >  how to convert string to color in unity c#
how to convert string to color in unity c#

Time:09-03

I have converted a color in Unity c# to string

> Body_Color=carParts_colorParts[0].materials[0].GetColor("_MainColor").ToString();

Now the output value is

RGBA(1.000, 0.000, 0.000, 1.000)

I want to convert back this RGBA() to color.

how can we do this

CodePudding user response:

Easiest way to convert a color to string and back would be using ColorUtility.ToHtmlStringRGBA and ColorUtility.TryParseHtmlString.

If you want to parse the ToString() result manually you could do it with something like this:

string Body_Color = "RGBA(1.000, 0.000, 0.000, 1.000)";
string[] rgba = Body_Color.Substring(5, Body_Color.Length - 6).Split(", ");
Color color = new Color(float.Parse(rgba[0]), float.Parse(rgba[1]), float.Parse(rgba[2]), float.Parse(rgba[3]));
  • Related