- Home /
convert string to color
Hi,
Is there a way to convert string to color.
I have a string veriable colorName, and I assigne it value such as "yellow". I want to use this value as a color.
Smt like that: material.color=colorname; Not material.color=color.yellow
Thanx for any help,
Answer by Dune2 · Oct 19, 2016 at 06:37 AM
This is an old post, but not satisfactorily answered.
In C# I created an extension class that converts the name to Color
public static class ColorExtensions
{
/// <summary>
/// Convert string to Color (if defined as a static property of Color)
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static Color ToColor(this string color)
{
return (Color)typeof(Color).GetProperty(color.ToLowerInvariant()).GetValue(null, null);
}
}
You'd use it like
Color yellow = "yellow'.ToColor();
Answer by deadlyfingers · Feb 27, 2017 at 11:41 PM
Use "ColorUtility.TryParseHtmlString" to convert a color string: https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html
Color MyColour = Color.clear; ColorUtility.TryParseHtmlString ("yellow", out MyColour);
Answer by DaveA · Sep 25, 2012 at 11:10 PM
http://msdn.microsoft.com/en-us/library/essfb559.aspx has an example. It supplies the Color enum, you don't need to.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Convert string to material 0 Answers
Get JSON array object string value 2 Answers