- Home /
Changing color of a String
Hi! I am trying to get a string to appear as a different color as the rest of the text. Is there a "not to complicated" way of doing this? Here's my script:
if(k.Display == true)
{
GUI.Label(new Rect(Screen.width/2, k.Row * rowHeight, Screen.width * (3.0f/2.3f), Screen.height * (0.3f/6.3f)), "You terminated " + k.Killed);
}
I would like to have k.Killed in, lets say, blue. Anybody know how I could do that? Thank you in advance!
Answer by ArkaneX · Dec 08, 2013 at 08:55 PM
Use Unity Rich Text formatting syntax. In your case, you can replace
"You terminated " + k.Killed
with
"You terminated <color=blue>" + k.Killed + "</color>"
or
string.Format("You terminated <color=blue>{0}</color>", k.Killed)
Answer by FlightOfOne · Jun 07, 2018 at 04:15 PM
Here's one more:
public static string ColorString(string text, Color color)
{
return "<color=#" + ColorUtility.ToHtmlStringRGBA(color) + ">" + text + "</color>";
}
Answer by GameVortex · Dec 08, 2013 at 01:23 PM
Use GUI.color for that.
Edit: Oh, for the rest of the text, not the entire string, did not see that first. Then you need to do it separately. Write the first part of the string with one Label, then change color, and write the colored part with another Label.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to put a list of strings in a Text Unity 4.6 UI element 3 Answers
Can I create a list with an int/float and a string? C# 2 Answers
Convert String to int 1 Answer