From when I installed 5.5 I got this odd problem on fonts created at runtime where sometime they don’t show at all, or show one digit, or the full font at random.
I get no warnings or errors of any sort, this happen with both using the deprecated variables of characterInfo and the new ones. I read the fonts from external file in a custom binary format I wrote, here is the structure of it, I left also the deprecated stuff commented out:
using UnityEngine;
using NM.Engine;
using NM.Framework;
namespace NM.Format
{
[System.Serializable]
public class Font : ID
{
private UnityEngine.Font _font;
//Constructor
public Font () { }
// #pragma warning disable 0618
//Constructor args
public Font (string id)
{
this.id = id;
if(NM.IO.Path.FileExists(NM.IO.Path.fontPath + id, NM.IO.File.nmf))
{
string _path = NM.IO.Path.GetExistingPath (NM.IO.Path.fontPath + id + NM.IO.File.nmf);
using(System.IO.FileStream fs = new System.IO.FileStream(_path, System.IO.FileMode.Open))
{
using(System.IO.BinaryReader br = new System.IO.BinaryReader(fs))
{
_font = new UnityEngine.Font ();
if(!NM.IO.File.FileIsFont(br.ReadString ()))
{
_font = Exception.nullFont(_path);
return;
}
_font.name = br.ReadString ();
string _mat = br.ReadString();
int _count = br.ReadInt32();
CharacterInfo[] _chars = new CharacterInfo[_count];
if(_count > 0)
{
for(int i = 0; i < _count; i++)
{
_chars[i].index = br.ReadInt32();
_chars [i].uvBottomLeft = new Vector2 (br.ReadSingle (), br.ReadSingle ());
_chars [i].uvBottomRight = new Vector2 (br.ReadSingle (), br.ReadSingle ());
_chars [i].uvTopLeft = new Vector2 (br.ReadSingle (), br.ReadSingle ());
_chars [i].uvTopRight = new Vector2 (br.ReadSingle (), br.ReadSingle ());
_chars [i].minX = br.ReadInt32 ();
_chars [i].minY = br.ReadInt32 ();
_chars [i].maxX = br.ReadInt32 ();
_chars [i].maxY = br.ReadInt32 ();
_chars [i].advance = br.ReadInt32 ();
_chars [i].bearing = br.ReadInt32 ();
/*
_chars[i].uv = new Rect(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
_chars[i].vert = new Rect(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
_chars[i].width = br.ReadSingle();
_chars[i].flipped = br.ReadBoolean();
*/
}
if(_chars != null && _chars.Length > 0)
_font.characterInfo = _chars;
}
else
{
_font = Exception.nullFont(_path);
return;
}
br.Close ();
_font.material = NM.Asset.GetMaterialCache (_mat).GetMaterial();
br.Close();
}
fs.Close();
}
}
else
{
string _path = NM.IO.Path.GetExistingPath (NM.IO.Path.fontPath + id + NM.IO.File.nmf);
_font = Exception.nullFont(_path);
}
}
//Ritorna componente font
public UnityEngine.Font GetFont ()
{
if (_font)
return _font;
else
return Exception.nullFont (this.id);
}
}
}
I try debugging it and also applying the font material to other objects to see if it was a problem of reading the texture or anything, but even by comparing the created font to the source one in my project folder everything is identical and fine.
The font is not a ful lchars font, it only have numbers and isn’t dynamic. Adding the font from the project folder instead of creating it at runtime it works fine. I can’t pinpoint the problem with the runtime creation as everything goes fine and sometime when I hit Play it show up some other time it doesn’t but the data is all there.
As a side note I create the UI components at runtiem too from an xml file, so I apply the font to the Text component while creating the gui. The behaviour happen in both build and in editor.