Tutorials – Unity (ngui, and bitmap font to texture)

I made some tests with Unity 4 and several ways to use bitmap fonts. Most of the existing plugins are working very well,
for exemple ngui : Next-Gen UI kit (tested the free version only).

I would advice to follow the step by step tutorials on their page, then especially the tutorial 5 about the label. I won’t re-explain here what is already done, just follow the tutorial and it will work ! Most of the time, it will do the job you need.

However, i needed something different for a project: writing a bitmap font on a texture. Everything i tested worked pretty well but it’s using it’s own object, where you can for exemple stick a plane text to a wall. Now let’s say we have a mesh with an animation, some complexe transformation (maybe a flag waving, a page book turn), the text won’t curve like your mesh, so i wanted to create my own texture with a bitmap font, and apply it to my object.

Of course, you need to create this texture once, without updating your texte like a count down, or it will probably consume too much CPU.

I used several ressources i’ve found with google:
* an atlas font parser found on on http://code.google.com/p/scavenger-game/source/browse/trunk/ (it uses ngui c# scripts: bmglyph.vs bmfont.cs bmfontreader.cs). Basically it creates an object from the texte atlas file (produced for example with bmglyp/publish for unity)
* some ideas taken from this website: http://lukasz.dk/2011/05/01/pixel-perfect-bitmap-font-in-unity/. a pixel perfect bitmap font, which uses a spriteatlasmanager.

It showed me how to create textures and use Texture.SetPixels. The good part of that method, is that it’s working with the free version of Unity, and also works pretty well on iOS.

The script is currently under development, not really usable, but i plan to create a sample demo project for that method. But feel free to ask more details about it.

 I drop my script on an object, then i need to set the texture file, and the texture atlas (named arial61 on the screenshot above). The font texture must be read/write enabled. I also have a texte string (where is written ‘bmglyph texture’).

The material is using a Decal, where i will apply my generated texture.

void Start()

{

BMFontReader.Load(mFont, fontAtlas.name, fontAtlas.bytes);  //ngui atlas reader
txtTexture = CreatefillTexture2D(Color.clear, 1024, 1024); //the new texture
PrintTextToTexture( mFont,texte,50, 600, txtTexture); //write to the texture
txtTexture.Apply();
Material mat;
mat = gameObject.renderer.materials[materialTextIndex];
mat.SetTexture(“_DecalTex”,txtTexture);

}

As said before, i will post the full source code soon, i need to clean it before !