Dynamically Setting font size according to device Screen dpi

Hello everyone!

I am facing a problem with the font size for mobile devices.
I am testing on a device with 200 dpi(font size normal) and on a device with 300+ dpi, where the text appears too small.

Since there are too many devices each with a different dpi … Do I have to have 10-20 instances of the same font(which doesn’t sound to be the optimal solution).

Any hints?

For something simple, I just changed a setting on my Canvas.

In the Canvas Scaler (Script) section, Change the UI Scale Mode to “Scale With Screen Size”. That seemed to work for the simple game I am working on.

#Note!

Fortunately these days (2016+) Unity has ordinary dynamic type (type with sizes!) built-in. There is nothing to do.

Just drop in a .ttf file and away you go.

However often with games you want to use fancy type with glows, etc. To do this you just click the Unity menu item “Create Custom Font” and then you use something like GlyphDesigner to make your font.

#For custom (sprite sheet) fonts, there is a critical tip…

You must put in your project the awesome BitmapFontImporter by Benoit Freslon. Unity have not yet included a system for mapping a custom font, BitmapFontImporter does it perfectly. It’s a “must have”.

So that’s all there is to it these days.


#The old answers are of historic interest only, fortunately you can ignore all this…

Life used to be this hard!


Step one … buy something like GlyphDesigner. Do anything you want with typography. Red glows, whatever. Choose a full ASCII set or just a limited set. (For example if it is for a Score, you only need 0123456789.) That will make the sprite sheet smaller.

alt text

Click the export button on GlyphDesigner. Drop the two files in to your Unity project. Click “make new 2DTK font.”

Step two … just get TK2D from the asset store (if you don’t already). As well as being the 2D solution, it has a full typography solution thrown in as a bonus. Click to create a new font. Now just USE THAT IN YOUR PROJECT, just like any 3D object.

Never again think about screen resolution when doing text in a game. You can SEE exactly what you’re doing and just move it around like any thing else in the scene. (Use an ortho camera for it if relevant.) It has NO connection to the damned “gui” systems.

Regarding the “resolution independent” type in Unity. It does not exist. I suspect they mean it’s “resolution independent” if you do all the programming to make it work resolution independently.

Glyph designer: Glyph Designer - A Bitmap Font Generator for Mac · 71Squared or any similar product. 2DToolkit or another 3D text asset - asset store. Hope it helps

Dynamic fonts for mobile should be supported since Unity 4.0 but I’ve not tried it yet.

If you need another solution you could import the same non-dynamic font with different sizes for each resolution type and use a script to change the font on the fly. The class DisplayMetricsUtil provides GetResolutionType().

@edit:
There are 4 types, so you should import your font 4 times. Let’s say you want the font size set to 12dp, then import your fonts with size

  • 9 px (scale 0.75 - ldpi)
  • 12 px (scale 1 - mdpi)
  • 18 px (scale 1.5 - hdpi)
  • 24 px (scale 2 - xhdpi)

Set a switch on GetResolutionType() and assign the desired font.

The scales are described here: http://developer.android.com/guide/practices/screens_support.html
alt text