Exemplo n.º 1
0
        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);
                    return(textures);
                }

                TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
                texImporter.textureType   = TextureImporterType.GUI;
                texImporter.mipmapEnabled = false;
                texImporter.SaveAndReimport();
                textures[i] = texture;
            }
            return(textures);
        }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        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
        }