static void SetFontKerningItem(SerializedProperty item, BitmapCharKerning kerning)
    {
        //data prop map< pair<ushort,ushort>,float >
        item.Next(true);
        //Debug.Log(item.name + "[" + item.depth + "]" + "," + item.type + "," + item.hasChildren);
        //key pair <ushort,ushort> first,second
        item.Next(true);
        //前面都是内部数据,跳过
        //Debug.Log(item.name + "[" + item.depth + "]" + "," + item.type + "," + item.hasChildren);
        //key first
        item.Next(true);
        //Debug.Log(item.name + "[" + item.depth + "]" + "," + item.type + "," + item.hasChildren);

        item.intValue = kerning.firstChar;
        //key second
        item.Next(true);
        //Debug.Log(item.name + "[" + item.depth + "]" + "," + item.type + "," + item.hasChildren);

        item.intValue = kerning.secondChar;
        //second
        item.Next(true);
        //Debug.Log(item.name + "[" + item.depth + "]" + "," + item.type + "," + item.hasChildren);

        item.floatValue = kerning.amount;
    }
Example #2
0
    /* .fnt parser taken from
     *
     */
    static BitmapFont UpdateBitmapFont(string path, BitmapFont fnt)
    {
        XmlDocument doc = new XmlDocument();

        doc.Load(path);

        //Read font info
        XmlNode info = doc.SelectSingleNode("/font/info");

        fnt.Size = ReadFloatAttribute(info, "size");

        XmlNode common = doc.SelectSingleNode("/font/common");

        fnt.LineHeight = ReadFloatAttribute(common, "lineHeight");
        fnt.Base       = ReadFloatAttribute(common, "base");
        fnt.ScaleH     = ReadFloatAttribute(common, "scaleW");
        fnt.ScaleW     = ReadFloatAttribute(common, "scaleH");

        //Read character info
        XmlNodeList chars = doc.SelectNodes("/font/chars/char");

        fnt.Chars = new BitmapChar[chars.Count];
        int index = 0;

        foreach (XmlNode char_node in chars)
        {
            fnt.Chars[index]          = new BitmapChar();
            fnt.Chars[index].Id       = ReadIntAttribute(char_node, "id");
            fnt.Chars[index].Position = ReadVector2Attributes(char_node, "x", "y");
            fnt.Chars[index].Size     = ReadVector2Attributes(char_node, "width", "height");
            fnt.Chars[index].Offset   = ReadVector2Attributes(char_node, "xoffset", "yoffset");
            fnt.Chars[index].XAdvance = ReadFloatAttribute(char_node, "xadvance");
            fnt.Chars[index].Page     = ReadIntAttribute(char_node, "page");
            index++;
        }

        //Load texture pages and convert to distance fields
        XmlNodeList pages = doc.SelectNodes("/font/pages/page");

        Texture2D[] texturePages = new Texture2D[pages.Count];
        index = 0;
        foreach (XmlNode page in pages)
        {
            //Find original font texture
            string    imagePath    = System.IO.Path.GetDirectoryName(path) + "/" + page.Attributes["file"].Value;
            Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));

            //Make sure font texture is readable
            TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath);
            inputTextureImp.textureType    = TextureImporterType.Advanced;
            inputTextureImp.isReadable     = true;
            inputTextureImp.maxTextureSize = 4096;
            AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport);

            //Create distance field from texture
            Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor);
            texturePages[index] = distanceField;

            index++;
        }

        //Create texture atlas
        Texture2D pageAtlas = new Texture2D(0, 0);

        fnt.PageOffsets = pageAtlas.PackTextures(texturePages, 0);

        //Save atlas as png
        byte[] pngData    = pageAtlas.EncodeToPNG();
        string outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.png";

        System.IO.File.WriteAllBytes(outputPath, pngData);
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Set correct texture format
        TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);

        texImp.textureType   = TextureImporterType.Advanced;
        texImp.isReadable    = true;
        texImp.textureFormat = TextureImporterFormat.Alpha8;
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Load the saved texture atlas
        fnt.PageAtlas = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D));

        //Load kerning info
        XmlNodeList kernings = doc.SelectNodes("/font/kernings/kerning");

        fnt.Kernings = new BitmapCharKerning[kernings.Count];
        index        = 0;
        foreach (XmlNode kerning in kernings)
        {
            BitmapCharKerning krn = new BitmapCharKerning();
            krn.FirstChar       = ReadIntAttribute(kerning, "first");
            krn.SecondChar      = ReadIntAttribute(kerning, "second");
            krn.Amount          = ReadFloatAttribute(kerning, "amount");
            fnt.Kernings[index] = krn;
            index++;
        }

        return(fnt);
    }
    /* .fnt parser taken from
     *
     */
    static BitmapFont UpdateBitmapFont(string path, BitmapFont fnt)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(path);

        //Read font info
        XmlNode info = doc.SelectSingleNode("/font/info");
        fnt.Size = ReadFloatAttribute(info, "size");

        XmlNode common = doc.SelectSingleNode("/font/common");
        fnt.LineHeight = ReadFloatAttribute(common, "lineHeight");
        fnt.Base = ReadFloatAttribute(common, "base");
        fnt.ScaleH = ReadFloatAttribute(common, "scaleW");
        fnt.ScaleW = ReadFloatAttribute(common, "scaleH");

        //Read character info
        XmlNodeList chars = doc.SelectNodes("/font/chars/char");
        fnt.Chars = new BitmapChar[chars.Count];
        int index = 0;
        foreach (XmlNode char_node in chars)
        {
            fnt.Chars[index] = new BitmapChar();
            fnt.Chars[index].Id = ReadIntAttribute(char_node, "id");
            fnt.Chars[index].Position = ReadVector2Attributes(char_node, "x", "y");
            fnt.Chars[index].Size = ReadVector2Attributes(char_node, "width", "height");
            fnt.Chars[index].Offset = ReadVector2Attributes(char_node, "xoffset", "yoffset");
            fnt.Chars[index].XAdvance = ReadFloatAttribute(char_node, "xadvance");
            fnt.Chars[index].Page = ReadIntAttribute(char_node, "page");
            index++;
        }

        //Load texture pages and convert to distance fields
        XmlNodeList pages = doc.SelectNodes("/font/pages/page");
        Texture2D[] texturePages = new Texture2D[pages.Count];
        index = 0;
        foreach (XmlNode page in pages)
        {
            //Find original font texture
            string imagePath = System.IO.Path.GetDirectoryName(path) + "/" + page.Attributes["file"].Value;
            Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));

            //Make sure font texture is readable
            TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath);
            inputTextureImp.textureType = TextureImporterType.Advanced;
            inputTextureImp.isReadable = true;
            inputTextureImp.maxTextureSize = 4096;
            AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport);

            //Create distance field from texture
            Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor);
            texturePages[index] = distanceField;

            index++;
        }

        //Create texture atlas
        Texture2D pageAtlas = new Texture2D(0, 0);
        fnt.PageOffsets = pageAtlas.PackTextures(texturePages, 0);

        //Save atlas as png
        byte[] pngData = pageAtlas.EncodeToPNG();
        string outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.png";
        System.IO.File.WriteAllBytes(outputPath, pngData);
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Set correct texture format
        TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);
        texImp.textureType = TextureImporterType.Advanced;
        texImp.isReadable = true;
        texImp.textureFormat = TextureImporterFormat.Alpha8;
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Load the saved texture atlas
        fnt.PageAtlas = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D));

        //Load kerning info
        XmlNodeList kernings = doc.SelectNodes("/font/kernings/kerning");
        fnt.Kernings = new BitmapCharKerning[kernings.Count];
        index = 0;
        foreach (XmlNode kerning in kernings)
        {
            BitmapCharKerning krn = new BitmapCharKerning();
            krn.FirstChar = ReadIntAttribute(kerning, "first");
            krn.SecondChar = ReadIntAttribute(kerning, "second");
            krn.Amount = ReadFloatAttribute(kerning, "amount");
            fnt.Kernings[index] = krn;
            index++;
        }

        return fnt;
    }