Unity 创建fnt字体

已有 .fnt 和 .png 文件,通过 .fnt文件中的配置信息,读取到 .png文件中的每一个字符的大小位置,将读取到的信息填入自定义字体。

链接

UGUI 如何使用CustomFont(自定义字体)
Unity的UGUI中使用CustomFont(BMFont)

代码

读取 .fnt文件信息,并将信息填入自定义字体的Character Rects属性中:

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

// 创建bmfont
public class CreateFontEditor : Editor
{
    [MenuItem("Assets/CreateBMFont")]  
    static void CreateFont()
    {  
        Object obj = Selection.activeObject;  
        string fntPath = AssetDatabase.GetAssetPath(obj); 
        if (fntPath.IndexOf(".fnt") == -1)
        {  
            // 不是字体文件 
            Debug.LogError("The Selected Object Is Not A .fnt file!");
            return;  
        }  

        string customFontPath = fntPath.Replace(".fnt", ".fontsettings");  
        if (!File.Exists(customFontPath))
        {  
            Debug.LogError("The .fontsettings file not exists");
            return;  
        }  

        Debug.Log(fntPath);  
        StreamReader reader = new StreamReader(new FileStream(fntPath, FileMode.Open));  

        List<CharacterInfo> charList = new List<CharacterInfo>();  

        Regex reg = new Regex(@"char id=(?<id>\d+)\s+x=(?<x>\d+)\s+y=(?<y>\d+)\s+width=(?<width>\d+)\s+height=(?<height>\d+)\s+xoffset=(?<xoffset>(-|\d)+)\s+yoffset=(?<yoffset>(-|\d)+)\s+xadvance=(?<xadvance>\d+)\s+");  
        string line = reader.ReadLine();  
        int lineHeight = 65;  
        int texWidth = 512;  
        int texHeight = 512;  

        while (line != null)
        {  
            if (line.IndexOf("char id=") != -1)
            {  
                Match match = reg.Match(line);  
                if (match != Match.Empty)
                {  
                    var id = System.Convert.ToInt32(match.Groups["id"].Value);  
                    var x = System.Convert.ToInt32(match.Groups["x"].Value);  
                    var y = System.Convert.ToInt32(match.Groups["y"].Value);  
                    var width = System.Convert.ToInt32(match.Groups["width"].Value);  
                    var height = System.Convert.ToInt32(match.Groups["height"].Value);  
                    var xoffset = System.Convert.ToInt32(match.Groups["xoffset"].Value);  
                    var yoffset = System.Convert.ToInt32(match.Groups["yoffset"].Value);  
                    var xadvance = System.Convert.ToInt32(match.Groups["xadvance"].Value);  
                    Debug.Log("ID" + id);


                    CharacterInfo info = new CharacterInfo();  
                    info.index = id;  
//                    float uvx = 1f * x / texWidth;  
//                    float uvy = 1 - (1f * y / texHeight);  
//                    float uvw = 1f * width / texWidth;  
//                    float uvh = -1f * height / texHeight;  
//
//                    info.uvBottomLeft = new Vector2(uvx, uvy);  
//                    info.uvBottomRight = new Vector2(uvx + uvw, uvy);  
//                    info.uvTopLeft = new Vector2(uvx, uvy + uvh);  
//                    info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh);  

//                    info.minX = xoffset;  
//                    info.minY = yoffset + height / 2;   // 这样调出来的效果是ok的,原理未知  
//                    info.glyphWidth = width;  
//                    info.glyphHeight = -height; // 同上,不知道为什么要用负的,可能跟unity纹理uv有关  
//                    info.advance = xadvance;  

                    info.uv.x = (float) x / texWidth;
                    info.uv.y = (float) y / texHeight;
                    info.uv.width = (float) width / texWidth;
                    info.uv.height = (float) height / texHeight;

                    info.vert.x = xoffset;
                    info.vert.y = yoffset;
                    info.vert.width = width;
                    info.vert.height = height;

                    info.advance = xadvance;

                    charList.Add(info);  
                }  
            }
            else if (line.IndexOf("scaleW=") != -1)
            {  
                Regex reg2 = new Regex(@"common lineHeight=(?<lineHeight>\d+)\s+.*scaleW=(?<scaleW>\d+)\s+scaleH=(?<scaleH>\d+)");  
                Match match = reg2.Match(line);  
                if (match != Match.Empty)
                {  
                    lineHeight = System.Convert.ToInt32(match.Groups["lineHeight"].Value);  
                    texWidth = System.Convert.ToInt32(match.Groups["scaleW"].Value);  
                    texHeight = System.Convert.ToInt32(match.Groups["scaleH"].Value);  
                }  
            }  
            line = reader.ReadLine();  
        }  

        Font customFont = AssetDatabase.LoadAssetAtPath<Font>(customFontPath);  
        customFont.characterInfo = charList.ToArray();  
        AssetDatabase.SaveAssets();  
        AssetDatabase.Refresh();  
        Debug.Log(customFont);  
    }
}  

自定义字体属性

这里写图片描述

.fnt 文件

info face="方正北魏楷书简体" size=65 bold=1 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=2,2,2,2 spacing=1,1 outline=1
common lineHeight=65 base=51 scaleW=512 scaleH=512 pages=1 packed=0 alphaChnl=0 redChnl=4 greenChnl=4 blueChnl=4
page id=0 file="65_number_baoji.png"
chars count=53
char id=33   x=493   y=0     width=15    height=46    xoffset=1     yoffset=9     xadvance=16    page=0  chnl=15
char id=34   x=138   y=230   width=25    height=25    xoffset=0     yoffset=9     xadvance=25    page=0  chnl=15
char id=37   x=116   y=182   width=48    height=47    xoffset=-2    yoffset=9     xadvance=43    page=0  chnl=15
char id=39   x=164   y=230   width=14    height=25    xoffset=-1    yoffset=9     xadvance=11    page=0  chnl=15
char id=40   x=471   y=0     width=21    height=59    xoffset=1     yoffset=7     xadvance=19    page=0  chnl=15
char id=41   x=227   y=63    width=21    height=59    xoffset=-3    yoffset=7     xadvance=19    page=0  chnl=15
char id=42   x=102   y=233   width=35    height=33    xoffset=-2    yoffset=8     xadvance=31    page=0  chnl=15
char id=43   x=62    y=234   width=39    height=38    xoffset=1     yoffset=13    xadvance=40    page=0  chnl=15
char id=45   x=298   y=224   width=39    height=10    xoffset=1     yoffset=27    xadvance=40    page=0  chnl=15
char id=47   x=401   y=176   width=30    height=45    xoffset=-1    yoffset=10    xadvance=28    page=0  chnl=15
char id=48   x=465   y=119   width=34    height=46    xoffset=-2    yoffset=9     xadvance=30    page=0  chnl=15
char id=49   x=34    y=234   width=27    height=44    xoffset=1     yoffset=10    xadvance=30    page=0  chnl=15
char id=50   x=234   y=178   width=34    height=45    xoffset=-2    yoffset=9     xadvance=30    page=0  chnl=15
char id=51   x=269   y=178   width=33    height=45    xoffset=-2    yoffset=10    xadvance=30    page=0  chnl=15
char id=52   x=199   y=178   width=34    height=45    xoffset=-2    yoffset=10    xadvance=30    page=0  chnl=15
char id=53   x=369   y=176   width=31    height=45    xoffset=-1    yoffset=10    xadvance=30    page=0  chnl=15
char id=54   x=165   y=178   width=33    height=46    xoffset=-1    yoffset=9     xadvance=30    page=0  chnl=15
char id=55   x=0     y=234   width=33    height=44    xoffset=-2    yoffset=11    xadvance=30    page=0  chnl=15
char id=56   x=336   y=177   width=32    height=45    xoffset=-1    yoffset=10    xadvance=30    page=0  chnl=15
char id=57   x=303   y=177   width=32    height=45    xoffset=-1    yoffset=10    xadvance=30    page=0  chnl=15
char id=58   x=488   y=166   width=15    height=32    xoffset=1     yoffset=23    xadvance=16    page=0  chnl=15
char id=61   x=179   y=225   width=39    height=21    xoffset=1     yoffset=20    xadvance=40    page=0  chnl=15
char id=63   x=463   y=174   width=24    height=45    xoffset=-1    yoffset=10    xadvance=22    page=0  chnl=15
char id=91   x=144   y=123   width=20    height=57    xoffset=2     yoffset=9     xadvance=19    page=0  chnl=15
char id=92   x=432   y=174   width=30    height=45    xoffset=-1    yoffset=10    xadvance=28    page=0  chnl=15
char id=93   x=123   y=123   width=20    height=57    xoffset=-3    yoffset=9     xadvance=19    page=0  chnl=15
char id=95   x=259   y=224   width=38    height=11    xoffset=-3    yoffset=50    xadvance=31    page=0  chnl=15
char id=126  x=219   y=224   width=39    height=17    xoffset=-3    yoffset=24    xadvance=33    page=0  chnl=15
char id=20013 x=55    y=0     width=52    height=63    xoffset=6     yoffset=-2    xadvance=61    page=0  chnl=15
char id=20912 x=49    y=64    width=62    height=59    xoffset=0     yoffset=0     xadvance=61    page=0  chnl=15
char id=20923 x=171   y=63    width=55    height=59    xoffset=1     yoffset=0     xadvance=61    page=0  chnl=15
char id=20987 x=63    y=124   width=59    height=57    xoffset=1     yoffset=0     xadvance=61    page=0  chnl=15
char id=21360 x=416   y=0     width=54    height=60    xoffset=2     yoffset=1     xadvance=61    page=0  chnl=15
char id=22238 x=63    y=182   width=52    height=50    xoffset=4     yoffset=5     xadvance=61    page=0  chnl=15
char id=22797 x=235   y=0     width=60    height=62    xoffset=1     yoffset=-2    xadvance=61    page=0  chnl=15
char id=23450 x=230   y=123   width=61    height=54    xoffset=1     yoffset=2     xadvance=61    page=0  chnl=15
char id=23553 x=353   y=0     width=62    height=60    xoffset=-1    yoffset=0     xadvance=61    page=0  chnl=15
char id=24341 x=0     y=64    width=48    height=60    xoffset=4     yoffset=0     xadvance=61    page=0  chnl=15
char id=25377 x=0     y=125   width=62    height=57    xoffset=-1    yoffset=2     xadvance=61    page=0  chnl=15
char id=26197 x=0     y=0     width=54    height=63    xoffset=4     yoffset=-2    xadvance=61    page=0  chnl=15
char id=26292 x=172   y=0     width=62    height=62    xoffset=-1    yoffset=-1    xadvance=61    page=0  chnl=15
char id=26684 x=439   y=61    width=66    height=57    xoffset=-3    yoffset=1     xadvance=61    page=0  chnl=15
char id=27602 x=108   y=0     width=63    height=62    xoffset=-2    yoffset=-2    xadvance=61    page=0  chnl=15
char id=27969 x=345   y=122   width=61    height=53    xoffset=0     yoffset=3     xadvance=61    page=0  chnl=15
char id=28796 x=380   y=61    width=58    height=58    xoffset=0     yoffset=1     xadvance=61    page=0  chnl=15
char id=28857 x=112   y=63    width=58    height=59    xoffset=0     yoffset=0     xadvance=61    page=0  chnl=15
char id=28903 x=316   y=63    width=63    height=58    xoffset=-2    yoffset=0     xadvance=61    page=0  chnl=15
char id=29123 x=249   y=63    width=66    height=58    xoffset=-3    yoffset=1     xadvance=61    page=0  chnl=15
char id=30505 x=407   y=120   width=57    height=53    xoffset=2     yoffset=3     xadvance=61    page=0  chnl=15
char id=34880 x=0     y=183   width=62    height=50    xoffset=-1    yoffset=3     xadvance=61    page=0  chnl=15
char id=36523 x=296   y=0     width=56    height=62    xoffset=2     yoffset=-2    xadvance=61    page=0  chnl=15
char id=36991 x=165   y=123   width=64    height=54    xoffset=-1    yoffset=2     xadvance=61    page=0  chnl=15
char id=38378 x=292   y=122   width=52    height=54    xoffset=4     yoffset=3     xadvance=61    page=0  chnl=15

对应 .png文件

.png文件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值