简体   繁体   中英

how to convert string to color in unity c#

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

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]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM