Search Unity

Custom Font - fill the m_CharacterRects vector - assign texture char position to it.

Discussion in 'Scripting' started by Matt_001, Oct 8, 2010.

  1. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    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 ?

    Code (csharp):
    1.  
    2.         Debug.Log("Load optimized Font.");
    3.     AssetDatabase.CopyAsset("Assets/Resources/Fonts/Template.fontsettings", "Assets/Resources/Fonts/OptimizedFont.fontsettings");
    4.     AssetDatabase.Refresh();
    5.     Font customFont = (Font)AssetDatabase.LoadAssetAtPath("Assets/Resources/Fonts/OptimizedFont.fontsettings", typeof(Font));
    6.     SerializedObject serializedFont = new SerializedObject(customFont);
    7.    
    8.         FindProperty(serializedFont, "m_Kerning").floatValue = 1;
    9.         FindProperty(serializedFont, "m_LineSpacing").floatValue = 1;
    10.  
    11.     //Load the current texture in a temp one in order to use Texture2D.GetPixels
    12.     Texture2D myTexture = new Texture2D(1,1);
    13.        
    14.     Debug.Log("Load the texture");
    15.     byte[] buffer = System.IO.File.ReadAllBytes("Assets/Resources/Fonts/OptimizedFont.png");
    16.     myTexture.LoadImage(buffer);
    17.        
    18.     //Custom Class that fill the properties I need in order to fill the CharacterRects
    19.         CustomTexture myTextureInfo = new CustomTexture( buffer );
    20.    
    21.     FindProperty(serializedFont, "m_GridFont").boolValue = false;
    22.     FindProperty(serializedFont, "m_CharacterRects.Array.size").intValue = myTextureInfo.glyphs.count();
    23.    
    24.         //A Glyph is my custom class to represent character position in the texture, glyphs is a list.
    25.     foreach(Glyph g in myTextureInfo.glyphs)
    26.     {
    27.         int index = flightFont.glyphs.IndexOf(g);
    28.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.x").floatValue = g.x;
    29.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.y").floatValue = g.y;
    30.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.width").floatValue = g.width;
    31.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].uv.height").floatValue = g.height;
    32.            
    33.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.x").floatValue = g.x;
    34.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.y").floatValue = g.y;
    35.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.width").floatValue = g.width;
    36.         FindProperty(serializedFont, "m_CharacterRects.Array.data[" + index + "].vert.height").floatValue = g.height;
    37.     }
    38.                
    39.     Material mat = new Material(Shader.Find("CustomShader"));
    40.     mat.mainTexture = myTexture;
    41.     AssetDatabase.CreateAsset(mat, "Assets/Resources/Fonts/OptimizedFont_material.mat");
    42.  
    43.     FindProperty(serializedFont, "m_DefaultMaterial").objectReferenceValue = mat;
    44.     FindProperty(serializedFont, "m_Texture").objectReferenceValue = myTexture;
    45.        
    46.     serializedFont.ApplyModifiedProperties();
    47.     Debug.Log("Optimized import test done.");
    48.  
    49.         private SerializedProperty FindProperty (SerializedObject obj, string name)
    50.     {
    51.         return obj.FindProperty(name);
    52.     }
    53.  
    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.
     
  2. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    Bump! I cannot believe there is no one who can help me with this...