Unity Default Font Size Returns 0

Hi,

I’m trying to get the size of a font in scripting, its a custom character set font with a fixed size of 32. I could put in the data manually but I’d like to automate it.

The thing I can’t figure out is how to get the font size, I’ve tried to use the fonts CharacterInfo.size but it always returns 0 as it says in the documentation, this thing I cant find in the documentation is how to get the default size. Any ideas?

You can get that information from the FontImporter.

public Font TheFont; // Reference to the font you need the information on.

// Then somewhere in your code

        string fontPath = AssetDatabase.GetAssetPath(TheFont);  // Get the path of that asset

        TrueTypeFontImporter fontImporter = (TrueTypeFontImporter)AssetImporter.GetAtPath(fontPath);

        Debug.Log(fontImporter.fontSize);

Basically, when a font is imported a size is assigned to it and that is the default. Not sure why Unity needs to report default size as Zero instead of what it is but this will allow you to get to what the default is.

Hope this helps.

Cheers Stephan, because I wanted to access is at runtime without needing to add extra properties I ended up getting the font size from the importer using your code from above and setting that size as the font name, then parsing that at runtime, its a bit messy but it works.

Glad I could help :slight_smile:

And you helped me too :wink:
Thanks a lot for the tip!!!