Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

GUI.skin - How to set font size and color?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Superflat, Jan 27, 2011.

  1. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Hello,

    I setup a skin for use with GUI.Label. I set a colour and a size on the normal state of the Label. The color works but the size does not. Why is that?

    Incidentally, i was looking over the Unity Answers site for some info on this and they suggest to use a GUI style. Doesn't this over write the skin?

    How can i set the colour and font size through a script while still using the skin?
     
  2. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    I have the same problem. I found several thread talking about GUI Labels, but I cannot "join" all together to make a GUI.Label using custom font size and color.

    I use this code, but does not work:

    Code (csharp):
    1. function OnGUI() {
    2.     var myStyle : GUIStyle = new GUIStyle();
    3.     myStyle.fontSize = 50;
    4.     GUI.color = Color.white;
    5.     GUI.Label (Rect (200, 200, 600, 100), "Creazione scenario...", myStyle);
    6. }
    In this way I get the correct font size but color is still black. If I do:

    Code (csharp):
    1. function OnGUI() {
    2.     GUI.color = Color.white;
    3.     GUI.Label (Rect (200, 200, 600, 100), "Creazione scenario...");
    4. }
    I get the correct color (white) but font size is small! :confused:

    Please give me an hint since I'm becoming mad!!!

    Thank you for your help! :)
     
  3. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    Hello,

    I just solved the "puzzle"!

    Here the final code (I hope this could help other people!):

    Code (csharp):
    1. function OnGUI() {
    2.     var myStyle : GUIStyle = new GUIStyle();
    3.     myStyle.fontSize = 50;
    4.     myStyle.normal.textColor = Color.white;
    5.     GUI.Label (Rect (200, 200, 600, 100), "Creazione scenario...", myStyle);
    6. }
    7.  
     
    jister likes this.
  4. Rigato

    Rigato

    Joined:
    Jun 17, 2011
    Posts:
    5
    As you guys suffered trying to change the size os the label, I was also mad about this, searching one way to make it work through GUI Skin.

    So I pulled it off with a simple "trick". Select the correct font at the GUI Skin inspector, and the resizing (and other stuff maybe?) should work.

    Cheers!
     
    Reedex likes this.
  5. marek428

    marek428

    Joined:
    Jul 6, 2011
    Posts:
    2
    I am using 3.3 and I have the same problem. I think there is a bug in the GUI.skin.label (or more likely my understanding on how this works)

    I tried setting the font size and color, but it does not work in the inspector for GUISkin -> Label (although it works for GUISkin -> TextField and others)

    So I though I could do it in code and tried the following in On GUI:
    //Changes only color
    GUIStyle localStyle = new GUIStyle(GUI.skin.label);
    localStyle.normal.textColor = Color.red;
    localStyle.fontSize = 12;

    //Changes both color and font size
    GUIStyle localStyle = new GUIStyle();
    localStyle.normal.textColor = Color.red;
    localStyle.fontSize = 50;

    It seems that as soon as try to set the font size specifically on the label, whether in the inspector or code, it does not work. I am a bit confused on this...
     
  6. TheFatController

    TheFatController

    Joined:
    Sep 20, 2010
    Posts:
    4
    I was having similar problems getting my GUILayout.Label() call to respect the value I was setting for GUISkin.Label.fontsize in the inspector.

    I tried setting GUISkin.Label.font to Arial (it was previously none) and now the fontsize value is used.

    Label()'s use of GUIStyle font settings does seem inconsistent with other controls.

    (just re-read Rigato's post and they have the same solution although I didn't quite follow it first time of reading)
     
  7. marjan

    marjan

    Joined:
    Jun 6, 2009
    Posts:
    563
    To my knowledge you cannot set the fontsize. Well, maybe you can, but that would just be scaling. I think you need to import the font with the desired fontsize.
    Eventually scaling down is fine. Means, you import the font with ponitsize 48, and the et it to 32, 16 or whatever.
    Not sure about that.

    But you definatly define the fontsize for best results with import settings.
     
  8. viveK-Dhayalan

    viveK-Dhayalan

    Joined:
    Sep 5, 2013
    Posts:
    2
    var Skin : GUISkin;

    function OnGUI () {
    GUI.skin = Skin;
    Skin.button.fontSize = Screen.width/25;


    }
    This is to change the font size for "button" , "label" should be similar.



    I did this for my mobile game . Hope it helps.:cool:
     
    Last edited: Feb 5, 2014
  9. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    Rigat
    o Nice one Thank You.