Unity font issue on iOS/Android

From ETC Public Wiki
Jump to: navigation, search

Font for iOS/Android

File:Font asset.png iOS/Android device does NOT support Dynamic font, which means you cannot adjust font's size, style in TextMesh component or GUIText component. Instead, you need to set these value in the font asset import settings. If you need variation with a different size or color, you need make another copy of that font asset and set those values. An example is shown on the right. Here is the steps you need for using font in Unity iOS/Android:

  1. Import font asset, here you will decide which style of font to be imported (normal, bold, italic, etc.).
  2. Go to font asset import settings, change Character to Unicode or ASCII, according to which encoding you are using. For Unicode font, more details will be given in the next session. Set Font Size and Font Color.
  3. Drag the font into TextMesh or GUIText component. For TextMesh, don't forget to drag font material (you can find it when collapse font asset) into MeshRender.
  4. In TextMesh or GUIText, set Font Size to 0, Font Style to Normal. The font size and style will be determined by the font asset.
  5. Build to your device and enjoy :)

Font for Asian Characters

File:Cygwin package.jpg

Issue

Using Asian characters in Unity3D for iOS/Android needs extra effort. Unity put every supported characters in a font to a bitmap for rendering, and iOS only supports up to 2048x2048 (1024x1024 for older devices) textures. It works fine for western language, but for Asian languages, you can imagine how many characters there are and how small each character will be to fit in that 2048x2048 bitmap. From the test of Team Locomobio, using a Japanese font with level 2 common characters set, the maximum size of the font can be 44 for iPhone 4S. And for iPhone 4, iPhone 3GS or Android device we have, the maximum size is smaller than 20. Even if we are ok with size 44, the font texture size can be as huge as 8.0MB, which is not acceptable of iOS app. You have to extra work to reduce the size of characters set.

Easy Solution

If you are using Japanese and most of your text is of kana or common kanji, you are lucky to go with easy solution.
If you are using Chinese or advanced Japanese kanji, please go ahead to next part for the full solution.
For easy solution, there is a free selected Japanese character font available on Unity Asset Store.
Go ahead to download this font, and follow the instruction on last section, you should be fine.

Full Solution

If you reached this part, you need to cut out characters you don't need on your own.
We use a tool called FontForge. Here is a complete steps for the trick.

Setup Cygwin and FontForge

This part is for windows user only, since FontForge need to run in a linux-like environment.
For other users, you can find instructions of FontForge website to setup your version.
For full instructions please refer to this page, I copied its steps here for convenience.

  1. Download the cygwin setup. Run the setup.exe and first choose your download mirror.
  2. Then choose the packages to be installed in the Cygwin Enviroment, you will need the default stuff, plus the X window system, binutils, libpng, libjpeg, and libxml2.
  3. Download FontForge and unpack the file(s) into a directory inside Cygwin Enviroment. Eg. C:\cygwin\Home\Fontforge\
  4. Run Cygwin.
  5. Run the X Window System required by Fontforge. Just type in the Cygwin Bash: $ startx
  6. Run Fontforge from the unpacked folder: $ run /home/fontforge/fontforge.bat

Edit the Font

First get a font. We use M+ Japanese font. Then open the font in FontForge, right click on characters you don't need and select clear. When you finished, go to File -> Generate Fonts, and select TrueType to generate a new font. Now you can import the new font into Unity. (Remember to set the Character to Unicode in import settings).
It's a huge amount of work to get ride of those characters by hand. In File -> Execute Script, we can type up scripts to ease our life.
An example script is as following, we select every characters except 'a' (0u61), and clear them:

   SelectNone()
SelectMore(0u61)
SelectInvert()
Clear()

In the next section, we provide a simple program to read in a text file and generate a script that select all characters that is not contained in that file and clear them. So you only need to put all characters you need to use in the game in to text file, and execute the script out put by the program. To execute external script, go to File -> Execute Script->Call.

UnicodeFontHelper

This program is a quick solution for Team Locomobio. It is not designed to be a complete tool. Please read the code and make sure you understand what is going on, and customize your version. Basically you just need to go \UnicodeFontHelper\UnicodeFontHelper\bin\Debug, put your characters in Characters.txt, run the program and it will output FontCut.pe as a script for FontForge.

Unicode Font Helper project