简体   繁体   English

如何在统一 c# 中将字符串转换为颜色

[英]how to convert string to color in unity c#

I have converted a color in Unity c# to string我已将 Unity c# 中的颜色转换为字符串

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

Now the output value is现在 output 值为

RGBA(1.000, 0.000, 0.000, 1.000) RGBA(1.000, 0.000, 0.000, 1.000)

I want to convert back this RGBA() to color.我想将此 RGBA() 转换回颜色。

how can we do this我们应该怎么做

Easiest way to convert a color to string and back would be using ColorUtility.ToHtmlStringRGBA and ColorUtility.TryParseHtmlString .将颜色转换为字符串并返回的最简单方法是使用ColorUtility.ToHtmlStringRGBAColorUtility.TryParseHtmlString

If you want to parse the ToString() result manually you could do it with something like this:如果你想手动解析 ToString() 结果,你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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