Greetings,
It’s been a whole week now since I’m trying to import full optimized custom font inside Unity. As we know, the current editor doesn’t allow non-grid custom font to be imported. However, we can use reflection to get properties of the custom font file. So, I want to be able to use my texture which contains my custom characters.
I’ve assigned each of my char inside the m_CharacterRects vector but I still don’t know how to make it work. Maybe someone has an idea ?
Debug.Log("Load optimized Font.");
AssetDatabase.CopyAsset("Assets/Resources/Fonts/Template.fontsettings", "Assets/Resources/Fonts/OptimizedFont.fontsettings");
AssetDatabase.Refresh();
Font customFont = (Font)AssetDatabase.LoadAssetAtPath("Assets/Resources/Fonts/OptimizedFont.fontsettings", typeof(Font));
SerializedObject serializedFont = new SerializedObject(customFont);
FindProperty(serializedFont, "m_Kerning").floatValue = 1;
FindProperty(serializedFont, "m_LineSpacing").floatValue = 1;
//Load the current texture in a temp one in order to use Texture2D.GetPixels
Texture2D myTexture = new Texture2D(1,1);
Debug.Log("Load the texture");
byte[] buffer = System.IO.File.ReadAllBytes("Assets/Resources/Fonts/OptimizedFont.png");
myTexture.LoadImage(buffer);
//Custom Class that fill the properties I need in order to fill the CharacterRects
CustomTexture myTextureInfo = new CustomTexture( buffer );
FindProperty(serializedFont, "m_GridFont").boolValue = false;
FindProperty(serializedFont, "m_CharacterRects.Array.size").intValue = myTextureInfo.glyphs.count();
//A Glyph is my custom class to represent character position in the texture, glyphs is a list.
foreach(Glyph g in myTextureInfo.glyphs)
{
int index = flightFont.glyphs.IndexOf(g);
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.x").floatValue = g.x;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.y").floatValue = g.y;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.width").floatValue = g.width;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.height").floatValue = g.height;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.x").floatValue = g.x;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.y").floatValue = g.y;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.width").floatValue = g.width;
FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.height").floatValue = g.height;
}
Material mat = new Material(Shader.Find("CustomShader"));
mat.mainTexture = myTexture;
AssetDatabase.CreateAsset(mat, "Assets/Resources/Fonts/OptimizedFont_material.mat");
FindProperty(serializedFont, "m_DefaultMaterial").objectReferenceValue = mat;
FindProperty(serializedFont, "m_Texture").objectReferenceValue = myTexture;
serializedFont.ApplyModifiedProperties();
Debug.Log("Optimized import test done.");
private SerializedProperty FindProperty (SerializedObject obj, string name)
{
return obj.FindProperty(name);
}
The problem is even if I set the whole thing. When I create a 3D text and I select the proper texture and material, nothing is displayed. Of course, I prolly missed something.
I’m wondering what “uv” and “vert” means in the CharacterRects vector. Also in that vector, there is a “width” property, which is on the same depth as “uv” and “vert”, and I’m wondering what is it purpose as well.
I’m still looking for a way to set the current ASCII char code of each character in the array. I haven’t find anything to do so, yet.
Also, thanks for Rouhee and his Font Importer from which I get my inspiration fo mine.