x


Change GUI Font size and Color?

How can I change the font and color of a GUI.Label? And other GUI objects?

I'm sorry if this has bean answered before, but all I found were questions about how to change the GameObject GUIText.

more ▼

asked Mar 27 '10 at 08:08 PM

Rufalo gravatar image

Rufalo
65 5 6 13

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

You need to supply a GUIStyle along with your draw call.

GUI.Label can take a style as a third parameter.

private void OnGUI()
{
    GUIStyle myStyle = new GUIStyle();
    myStyle.font = myFont;
    GUI.Label(new Rect(10,10, 100, 30), "Hello World!", myStyle);
}

The Font must be created as a game asset and can be assigned to the script via the property inspector. It can contain a material that will specify color.

As for creating the initial Font, it is often best to find a good ttf font that you like, import that, then assign a material with the appropriate color. This site has a bunch of great references on fonts.

more ▼

answered Mar 27 '10 at 08:28 PM

Michael La Voie gravatar image

Michael La Voie
2.4k 7 13 43

Thanks, this is helpful to know.

Mar 28 '10 at 04:35 AM Christian Stewart

Hi guys, this solutions seams to be pretty good.

I did everything you guys suggested, but it is not working.

1) I created the asset called myGuiSkin, then,

on the script: 2) public GUISkin guiSkin;

3) Then I dragged the asset to the field.

4) I put .TTF font in assets/font directory

5) I selected the desired font in the button,label and window "parts" of myGUISkin

6) didn't work, nothing changes - dont know why...

Mar 27 at 02:44 PM xpecttrum
(comments are locked)
10|3000 characters needed characters left

If you just want to change the color, you can use GUI.contentColor. (There's also GUI.color and GUI.backgroundColor.) You can scale content without having to use new styles or import fonts at different sizes by changing GUI.matrix, but it won't be pixel-perfect in that case.

more ▼

answered Mar 27 '10 at 08:39 PM

Eric5h5 gravatar image

Eric5h5
102k 49 165 620

@Eric5h5 you have mentioned that we can scale content without importing fonts at different sizes.. As I have asked here , and no one suggested anything about GUI.matrix .. can you explain a bit how to do that?

Mar 18 '13 at 06:13 PM moghes
(comments are locked)
10|3000 characters needed characters left

If you just want to change the font and color, you can do this:

public Font font;
public Color color;

//-------------------------------

void OnGUI () {

    GUI.skin.font = font;
    GUI.color = color;

    //----ZOOM IN BUTTON----///

    if (GUI.Button (new Rect (10,10,64,64), "+")) {

       ZoomIn();
    }
}

Just make a public variable called 'font' and drag the one you want into the Inspector, and pick your color from the Inspector too. You can keep all of the existing layout stuff this way.

more ▼

answered Jun 09 '12 at 09:34 PM

BobBobson108 gravatar image

BobBobson108
16 1 1 4

(comments are locked)
10|3000 characters needed characters left

First created a folder in Assets called Resources (Assets/Resources). And there I created an Folder Fonts.

Than

void OnGUI() {
    // Create style for a button
    GUIStyle myButtonStyle = new GUIStyle(GUI.skin.button);
    myButtonStyle.fontSize = 50;

    // Load and set Font
    Font myFont = (Font)Resources.Load("Fonts/comic", typeof(Font));
    myButtonStyle.font = myFont;

    // Set color for selected and unselected buttons
    myButtonStyle.normal.textColor = Color.red;
    myButtonStyle.hover.textColor = Color.red;

    ...

    // use style in button
    bool testButtonTwo = GUI.Button(new Rect(10,10,50,50), "test", myButtonStyle);

    ...
}
more ▼

answered Nov 29 '12 at 02:44 PM

jinxi gravatar image

jinxi
1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7806
x5097

asked: Mar 27 '10 at 08:08 PM

Seen: 47605 times

Last Updated: Mar 27 at 02:44 PM