How do I change the color of the text color in my GUI.Label? Lets say into black
function OnGUI ()
{
GUI.Label (Rect (400, 200, 200, 60), “color”);
}
How do I change the color of the text color in my GUI.Label? Lets say into black
function OnGUI ()
{
GUI.Label (Rect (400, 200, 200, 60), “color”);
}
You can do it by setting up a custom GUIStyle.
var style : GUIStyle;
Add this to the script. Then you can either tweak the color in the Inspector or code the color inside the script.
var style : GUIStyle;
function Start(){
style.normal.textColor = Color.black;
}
function OnGUI () {
GUI.Label (Rect(5,5,50,50), "GUI Label Color",style);
}
Use GUI.contentColor:
function OnGUI ()
{
GUI.contentColor = Color.black;
GUI.Label (Rect (400, 200, 200, 60), "color");
}
–Eric
Hi Eric5h5,
Jumping in with a related question. Is it possible to fade a GuiStyle Label with Lerp? I saw a thread where you faded GuiText, but I can’t get the syntax right to fade a Label.
Thanks.
Here’s your script for text from the other thread. I tried it, but get an error that material is not a member of GuiStyle:
var fadeDuration = 3.0;
function Start () {
yield Fade (0.0, 1.0, fadeDuration);
yield Fade (1.0, 0.0, fadeDuration);
Destroy(gameObject);
}
function Fade (startLevel, endLevel, time) {
var speed = 1.0/time;
for (t = 0.0; t < 1.0; t += Time.deltaTime*speed) {
guiText.material.color.a = Mathf.Lerp(startLevel, endLevel, t);
yield;
}
}
Edit:
I figured out a way that works. Don’t know if this is the most efficient. Took me a while to find a way to address the alpha channel, but this seems to do the job:
var timer:float;
function Fade ()
{
var fadetime = 1/timer;
var i : float = 0;
while (i<1.0)
{
i+=Time.deltaTime * fadetime;
var fade = Mathf.Lerp(1, 0, i);
highlight.normal.textColor = Color(0,.5,0,fade);
yield;
}
}
In some cases you may want to change only a specific word or sentence, so you just need to make your text like that:
sb.Append(“<size=10> <color=yellow>WARNING: Some text here”);
In this case my style font size is 8, so this text will be a bit bigger then the other lines, the word WARNING will be written in yellow and bold and ‘Some text here’ is the default color.
For more details check:
https://docs.unity3d.com/Manual/StyledText.html
Doesn’t work. Just … doesn’t.
We can color labels using the richtext as kalamtech described, but … either contentColor was never supported (and wasn’t supposed to work), or Unity broke it some time in the last year or so, because it is ignored in current Unity editor.
You do realize the post you’re responding to is literally a decade old, right? I don’t think Unity even had rich text then.
–Eric
GUI.contentColor does still function but it gets multiplied together with the textColor defined in the GUIStyleState. If the base text color is black then changing GUI.contentColor will have no effect, but otherwise it does work for tinting the text color.
Example:
private GUIStyle white;
private GUIStyle grey;
private GUIStyle black;
void OnEnable()
{
white = new GUIStyle(EditorStyles.label);
white.normal.textColor = Color.white;
grey = new GUIStyle(EditorStyles.label);
grey.normal.textColor = Color.grey;
black = new GUIStyle(EditorStyles.label);
black.normal.textColor = Color.black;
}
void OnGUI()
{
GUI.contentColor = Color.red;
GUILayout.Label("White", white); // fully red
GUILayout.Label("Grey", grey); // greyish red
GUILayout.Label("Black", black); // fully black
}
}
Result:
Oh, man. Yes, I think that’s it - an EditorStyle that’s using black as the color, and because it’s an in-built style there’s nothing in my code/project to show that the color is being wiped at source :). Gah. Good spot.
Hey man, does this work in light theme?
Yep, it work like this with both themes.
I tried it but for some reason it doesnt effect the light one, 2019 versions
Did you set GUIStyleState.textColor to white? What GUIStyle are you using? If you can post your code that isn’t working it’s easier to figure out where the issue lies
it work with the custom inspector ?
Yes.
Changing UI text color with GUI.contentColor works, but doing so, after restarting Unity will make some UI component text to black. (Changing the editor script code and reloading will fix though…)
The problem still exists on Unity 2021.
It’s better to change the text color with GUIStyle and not change it by GUI.contentColor at all.
public void OnGUI(){
{
GUIStyle boxStyle = new GUIStyle(GUI.skin.box);
boxStyle.normal.textColor = Color.white;
boxStyle.hover.textColor = Color.white;
GUILayout.Box("White Text", boxStyle);
boxStyle.normal.textColor = Color.green;
GUILayout.Box("Green Text", boxStyle);
}