private static Texture2D[] DoImportTextures(FntParse parse, string rootPath, TextAsset fnt) { int len = parse.textureNames.Length; Texture2D[] textures = new Texture2D[len]; for (int i = 0; i < len; i++) { // The texture name of the file generated by ShoeBox uses an absolute path string textureName = Path.GetFileName(parse.textureNames[i]); string texPath = string.Format("{0}/{1}", rootPath, textureName); Texture2D texture = AssetDatabase.LoadMainAssetAtPath(texPath) as Texture2D; if (texture == null) { Debug.LogErrorFormat(fnt, "{0}: not found '{1}'.", typeof(BFImporter), texPath); continue; } TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; texImporter.textureType = Settings.TextureImporterType; texImporter.mipmapEnabled = false; texImporter.wrapMode = TextureWrapMode.Clamp; texImporter.alphaIsTransparency = true; texImporter.alphaSource = TextureImporterAlphaSource.FromInput; texImporter.streamingMipmaps = false; // Sprite texImporter.spriteImportMode = SpriteImportMode.Single; texImporter.SaveAndReimport(); textures[i] = texture; } return(textures); }
public static FntParse GetFntParse(ref string text) { FntParse parse = null; if (text.StartsWith("info")) { parse = new FntParse(); parse.DoTextParse(ref text); } else if (text.StartsWith("<")) { parse = new FntParse(); parse.DoXMLPase(ref text); } return(parse); }
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 = fntName; 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_2018_4_OR_NEWER // No-op #elif UNITY_5_5_OR_NEWER // unity 5.5 can not load custom font ReloadFont(fontPath); #endif }