- Home /
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?
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... Unity also today offers sprite sheet fonts, which is great. But, you must put in your project the awesome BitmapFontImporter by Benoit Freslon. Unity have not yet included a system for mapping the data on a custom font, BitmapFontImporter does it perfectly. It's a "must have".
So that's all there is to it these days.
Answer by Fattie · Feb 19, 2013 at 01:44 PM
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".
https://github.com/BenoitFreslon/BitmapFontImporter
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.
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: http://www.71squared.com/en/glyphdesigner or any similar product. 2DToolkit or another 3D text asset - asset store. Hope it helps
[2]: /storage/temp/8035-results.jpg
Answer by jamiahuswalton · Apr 05, 2016 at 02:35 AM
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.
@jamiahuswalton Thanks for your answer , that solved my problem!
Note that these days, 2016, this answer is completely correct.
EVERYTHING on this page is only of historic interest. It is (finally) totally trivial to actually use type in Unity.
Answer by robhuhn · Feb 14, 2013 at 09:49 AM
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
Why don't you switch the font with the desired font size regarding what GetResolutionType() returns?
Just to be clear, if you don't use Unity 4 or later, you need to import the same font for every size. GetResolutionType() only helps to decide which font should be taken.
Actually you only check for the resolution type, not the resolution itself.
public class FontTest : $$anonymous$$onoBehaviour
{
//contains four fonts as described above - from ldpi [0] to xhdpi [3]
public List<Font> fonts = new List<Font>();
void Start ()
{
//get the font for the resolution type
Font desiredFont = fonts[(int)Display$$anonymous$$etricsUtil.GetResolutionType()];
myLabelStyle = myLabelStyle.DpToPixel(); //e.g. to adjust bounds
myLabelStyle.font = desiredFont;
}
}
That should be all you need.
Yes, but keep in $$anonymous$$d that this is deprecated since Unity 4 supports dynamic fonts on mobile.