Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

How to change text color on GUI.Label?

Discussion in 'Immediate Mode GUI (IMGUI)' started by michael tot korsgaard, Jun 17, 2010.

  1. michael tot korsgaard

    michael tot korsgaard

    Joined:
    Feb 11, 2010
    Posts:
    2
    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");
    }
     
  2. Ian88

    Ian88

    Joined:
    Jun 17, 2010
    Posts:
    4
    You can do it by setting up a custom GUIStyle.
    Code (csharp):
    1. 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.



    Code (csharp):
    1. var style : GUIStyle;
    2.  
    3. function Start(){
    4.     style.normal.textColor = Color.black;
    5.     }
    6. function OnGUI () {
    7.     GUI.Label (Rect(5,5,50,50), "GUI Label Color",style);
    8.    
    9. }
    10.  
     

    Attached Files:

  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,300
    Use GUI.contentColor:

    Code (csharp):
    1. function OnGUI ()
    2. {  
    3.     GUI.contentColor = Color.black;
    4.     GUI.Label (Rect (400, 200, 200, 60), "color");
    5. }
    --Eric
     
  4. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    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:

    Code (csharp):
    1. var fadeDuration = 3.0;
    2.  
    3. function Start () {
    4.    yield Fade (0.0, 1.0, fadeDuration);
    5.    yield Fade (1.0, 0.0, fadeDuration);
    6.    Destroy(gameObject);
    7. }
    8.  
    9. function Fade (startLevel, endLevel, time) {
    10.    var speed = 1.0/time;
    11.    
    12.    for (t = 0.0; t < 1.0; t += Time.deltaTime*speed) {
    13.       guiText.material.color.a = Mathf.Lerp(startLevel, endLevel, t);
    14.       yield;
    15.    }
    16. }
    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:

    Code (csharp):
    1.  
    2. var timer:float;
    3.  
    4. function Fade ()
    5.     {
    6.     var fadetime = 1/timer;
    7.     var i : float = 0;
    8.     while (i<1.0)
    9.     {
    10.     i+=Time.deltaTime * fadetime;
    11.     var fade = Mathf.Lerp(1, 0, i);
    12.     highlight.normal.textColor = Color(0,.5,0,fade);
    13.     yield;
    14.     }
    15.     }
     
  5. kalamtech

    kalamtech

    Joined:
    Oct 27, 2017
    Posts:
    8
    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><b>WARNING: </b></color> Some text here</size>");

    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
     
    wfowlerD9 and PiezPiedPy like this.
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,253
    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.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,300
    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
     
  8. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    495
    @a436t4ataf

    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:
    Code (CSharp):
    1.     private GUIStyle white;
    2.     private GUIStyle grey;
    3.     private GUIStyle black;
    4.  
    5.     void OnEnable()
    6.     {
    7.         white = new GUIStyle(EditorStyles.label);
    8.         white.normal.textColor = Color.white;
    9.  
    10.         grey = new GUIStyle(EditorStyles.label);
    11.         grey.normal.textColor = Color.grey;
    12.  
    13.         black = new GUIStyle(EditorStyles.label);
    14.         black.normal.textColor = Color.black;
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         GUI.contentColor = Color.red;
    20.         GUILayout.Label("White", white); // fully red
    21.         GUILayout.Label("Grey", grey);   // greyish red
    22.         GUILayout.Label("Black", black); // fully black
    23.     }
    24. }
    Result:
    GUI.contentColor.png
     
    a436t4ataf and lunaeight08 like this.
  9. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,253
    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.
     
  10. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    465
    Hey man, does this work in light theme?
     
  11. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    495
    Yep, it work like this with both themes.
     
  12. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    465
    I tried it but for some reason it doesnt effect the light one, 2019 versions
     
  13. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    495
    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 :)
     
  14. MintIsHere

    MintIsHere

    Joined:
    Nov 20, 2019
    Posts:
    34
    it work with the custom inspector ?
     
  15. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,253
unityunity