Skip to content

Commit 111e22b

Browse files
committed
fixed cannot render text and cannot create font material
1 parent 3ee9034 commit 111e22b

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

Assets/BitmapFontImporter/Editor/BFImporter.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,6 @@ public static void DoImportBitmapFont(string fntPatn)
4747
string fontPath = string.Format("{0}/{1}.fontsettings", rootPath, fntName);
4848
string texPath = string.Format("{0}/{1}", rootPath, parse.textureName);
4949

50-
Font font = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font;
51-
if (font == null)
52-
{
53-
font = new Font();
54-
AssetDatabase.CreateAsset(font, fontPath);
55-
font.material = new Material(Shader.Find("UI/Default"));
56-
font.material.name = "Font Material";
57-
AssetDatabase.AddObjectToAsset(font.material, font);
58-
}
59-
60-
SerializedObject so = new SerializedObject(font);
61-
so.Update();
62-
so.FindProperty("m_FontSize").floatValue = Mathf.Abs(parse.fontSize);
63-
so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight;
64-
so.FindProperty("m_Descent").floatValue = parse.lineBaseHeight - parse.lineHeight;
65-
so.FindProperty("m_Ascent").floatValue = parse.lineBaseHeight;
66-
UpdateKernings(so, parse.kernings);
67-
so.ApplyModifiedProperties();
68-
so.SetIsDifferentCacheDirty();
69-
70-
7150
Texture2D texture = AssetDatabase.LoadMainAssetAtPath(texPath) as Texture2D;
7251
if (texture == null)
7352
{
@@ -80,12 +59,41 @@ public static void DoImportBitmapFont(string fntPatn)
8059
texImporter.mipmapEnabled = false;
8160
texImporter.SaveAndReimport();
8261

83-
font.material.mainTexture = texture;
84-
font.material.mainTexture.name = "Font Texture";
85-
62+
63+
Font font = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font;
64+
if (font == null)
65+
{
66+
font = new Font();
67+
AssetDatabase.CreateAsset(font, fontPath);
68+
AssetDatabase.WriteImportSettingsIfDirty(fontPath);
69+
AssetDatabase.ImportAsset(fontPath);
70+
}
71+
Material material = AssetDatabase.LoadAssetAtPath(fontPath, typeof(Material)) as Material;
72+
if (material == null)
73+
{
74+
material = new Material(Shader.Find("UI/Default"));
75+
material.name = "Font Material";
76+
AssetDatabase.AddObjectToAsset(material, fontPath);
77+
// unity 5.4+ cannot refresh it immediately, must import it
78+
AssetDatabase.ImportAsset(fontPath);
79+
}
80+
font.material = material;
81+
material.mainTexture = texture;
8682
font.characterInfo = parse.charInfos;
83+
84+
SerializedObject so = new SerializedObject(font);
85+
so.Update();
86+
so.FindProperty("m_FontSize").floatValue = Mathf.Abs(parse.fontSize);
87+
so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight;
88+
so.FindProperty("m_Ascent").floatValue = parse.lineBaseHeight;
89+
SerializedProperty prop = so.FindProperty("m_Descent");
90+
if (prop != null)
91+
prop.floatValue = parse.lineBaseHeight - parse.lineHeight;
92+
UpdateKernings(so, parse.kernings);
93+
so.ApplyModifiedProperties();
94+
so.SetIsDifferentCacheDirty();
8795

88-
AssetDatabase.SaveAssets();
96+
AssetDatabase.SaveAssets();
8997

9098
#if UNITY_5_5_OR_NEWER
9199
// unity 5.5 can not load custom font

0 commit comments

Comments
 (0)