Search Unity

FontSize

Discussion in 'Scripting' started by Findo, Mar 28, 2014.

  1. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    I have spent numerous amounts of time trying to comprehend how to do this;
    the wiki has nothing good; nor the reference; but on google, I found this

    http://answers.unity3d.com/questions/13944/change-gui-font-size-and-color.html#

    Yet still, I was confused,
    this is what I have so far.

     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    font is the font that is uses

    try Style.fontSize
     
  3. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Create a new unity project. Make a c# script called fontsize.cs and paste the following code in:

    Code (csharp):
    1. // c# example
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class fontsize : MonoBehaviour {
    6.  
    7.     void OnGUI() {
    8.         GUIStyle g = GUI.skin.GetStyle("label");
    9.         g.fontSize = (int)(20.0f + 10.0f * Mathf.Sin(Time.time));
    10.         GUI.Label(new Rect(10, 10, 200, 80), "Hello World!");
    11.     }
    12. }
    As you probably know, the GUI system uses collections of styles grouped into skins. What this code does is simply grab the out-of-the-box style for the label component, and animate it's font size. When you run the code you'll note that the font doesn't smoothly change size because their are not an infinite number of font sizes. This relies on the default (Arial) font being loaded and marked as dynamic. You cannot change the size of any font that's not marked as dynamic.
     
  4. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    So... um..... can you explain line 9?
    I've never used Mathf :(
     
  5. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    1. Mathf is the library of maths functions in Unity.
    2. One of those functions is the Sin function, which, given an angle, returns the sin of that angle, which, if you recall your math lessons at school, is a number between plus and minus one.
    3. Rather than use an angle, I am just passing in the time in seconds. But the sin function doesn't know that. It just gives a a repeating number every 6 seconds or so.
    4. Because fonts have sizes in the range, say, 12pt to 32pt, I'm starting off with 20, and then adding +/-10 to that. So I end up with a number between 10 and 30, which I use to scale the font.