public static void DoImportBitmapFont(string fntPatn) { if (!IsFnt(fntPatn)) { return; } TextAsset fnt = AssetDatabase.LoadMainAssetAtPath(fntPatn) as TextAsset; string text = fnt.text; FntParse parse = FntParse.GetFntParse(ref text); if (parse == null) { return; } string fntName = Path.GetFileNameWithoutExtension(fntPatn); string rootPath = Path.GetDirectoryName(fntPatn); string fontPath = string.Format("{0}/{1}.fontsettings", rootPath, fntName); string texPath = string.Format("{0}/{1}", rootPath, parse.textureName); Font font = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font; if (font == null) { font = new Font(); AssetDatabase.CreateAsset(font, fontPath); font.material = new Material(Shader.Find("UI/Default")); font.material.name = "Font Material"; AssetDatabase.AddObjectToAsset(font.material, font); } SerializedObject so = new SerializedObject(font); so.Update(); so.FindProperty("m_FontSize").floatValue = parse.fontSize; so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight; UpdateKernings(so, parse.kernings); so.ApplyModifiedProperties(); so.SetIsDifferentCacheDirty(); Texture2D texture = AssetDatabase.LoadMainAssetAtPath(texPath) as Texture2D; if (texture == null) { //Debug.LogErrorFormat(fnt, "{0}: not found '{1}'.", typeof(BFImporter), texPath); return; } TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; texImporter.textureType = TextureImporterType.GUI; texImporter.mipmapEnabled = false; texImporter.SaveAndReimport(); font.material.mainTexture = texture; font.material.mainTexture.name = "Font Texture"; font.characterInfo = parse.charInfos; AssetDatabase.SaveAssets(); }
public static void DoImportBitmapFont(string fntPatn) { if (!IsFnt(fntPatn)) { return; } TextAsset fnt = AssetDatabase.LoadMainAssetAtPath(fntPatn) as TextAsset; string text = fnt.text; FntParse parse = FntParse.GetFntParse(ref text); if (parse == null) { return; } string fntName = Path.GetFileNameWithoutExtension(fntPatn); string rootPath = Path.GetDirectoryName(fntPatn); string fontPath = string.Format("{0}/{1}.fontsettings", rootPath, fntName); Texture2D[] textures = DoImportTextures(parse, rootPath, fnt); Font font = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font; if (font == null) { font = new Font(); AssetDatabase.CreateAsset(font, fontPath); AssetDatabase.WriteImportSettingsIfDirty(fontPath); AssetDatabase.ImportAsset(fontPath); } Material material = AssetDatabase.LoadAssetAtPath(fontPath, typeof(Material)) as Material; if (material == null) { material = new Material(Shader.Find("UI/Default")); material.name = "Font Material"; AssetDatabase.AddObjectToAsset(material, fontPath); // unity 5.4+ cannot refresh it immediately, must import it AssetDatabase.ImportAsset(fontPath); } font.material = material; material.shader = Shader.Find(textures.Length > 1 ? "BFI/Font" + textures.Length : "UI/Default"); material.mainTexture = textures[0]; for (int i = 1; i < textures.Length; i++) { material.SetTexture("_MainTex" + (i + 1), textures[i]); } font.characterInfo = parse.charInfos; SerializedObject so = new SerializedObject(font); so.Update(); so.FindProperty("m_FontSize").floatValue = Mathf.Abs(parse.fontSize); so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight; so.FindProperty("m_Ascent").floatValue = parse.lineBaseHeight; SerializedProperty prop = so.FindProperty("m_Descent"); if (prop != null) { prop.floatValue = parse.lineBaseHeight - parse.lineHeight; } UpdateKernings(so, parse.kernings); so.ApplyModifiedProperties(); so.SetIsDifferentCacheDirty(); AssetDatabase.SaveAssets(); #if UNITY_5_5_OR_NEWER // unity 5.5 can not load custom font ReloadFont(fontPath); #endif }
public static void DoImportBitmapFont(string fntPatn) { if (!IsFnt(fntPatn)) { return; } TextAsset fnt = AssetDatabase.LoadMainAssetAtPath(fntPatn) as TextAsset; string text = fnt.text; FntParse parse = FntParse.GetFntParse(ref text); if (parse == null) { return; } string fntName = Path.GetFileNameWithoutExtension(fntPatn); string rootPath = Path.GetDirectoryName(fntPatn); string fontPath = string.Format("{0}/{1}.fontsettings", rootPath, fntName); string texPath = string.Format("{0}/{1}", rootPath, parse.textureName); Texture2D texture = AssetDatabase.LoadMainAssetAtPath(texPath) as Texture2D; if (texture == null) { Debug.LogErrorFormat(fnt, "{0}: not found '{1}'.", typeof(BFImporter), texPath); return; } TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; texImporter.textureType = TextureImporterType.GUI; texImporter.mipmapEnabled = false; texImporter.SaveAndReimport(); Font font = AssetDatabase.LoadMainAssetAtPath(fontPath) as Font; if (font == null) { font = new Font(); AssetDatabase.CreateAsset(font, fontPath); AssetDatabase.WriteImportSettingsIfDirty(fontPath); AssetDatabase.ImportAsset(fontPath); } Material material = AssetDatabase.LoadAssetAtPath(fontPath, typeof(Material)) as Material; if (material == null) { material = new Material(Shader.Find("UI/Default")); material.name = "Font Material"; AssetDatabase.AddObjectToAsset(material, fontPath); // unity 5.4+ cannot refresh it immediately, must import it AssetDatabase.ImportAsset(fontPath); } font.material = material; material.mainTexture = texture; font.characterInfo = parse.charInfos; SerializedObject so = new SerializedObject(font); so.Update(); so.FindProperty("m_FontSize").floatValue = Mathf.Abs(parse.fontSize); so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight; so.FindProperty("m_Ascent").floatValue = parse.lineBaseHeight; SerializedProperty prop = so.FindProperty("m_Descent"); if (prop != null) { prop.floatValue = parse.lineBaseHeight - parse.lineHeight; } UpdateKernings(so, parse.kernings); so.ApplyModifiedProperties(); so.SetIsDifferentCacheDirty(); AssetDatabase.SaveAssets(); #if UNITY_5_5_OR_NEWER // unity 5.5 can not load custom font ReloadFont(fontPath); #endif }