已有 .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文件

Unity 创建fnt字体相关推荐

  1. Unity TextMeshpro创建中文字体

    使用Textmeshpro创建自定义中文字体 textmeshpro常用字体7000个 链接:https://pan.baidu.com/s/1nwLzZ7w0XOOi4wQ09Dl4Yg?pwd=p ...

  2. Unity 制作font字体

    有很多网友介绍Unity 创建并使用font的方法,这里总结一下备忘,同时网友的部分文章有一些小坑被我踩到,在这里记录一下,希望能帮助其他同学 一.首先创建字体(ttf文件),网上有用BitMap工具 ...

  3. unity制作bitmap字体-艺术字

    工具:bmfont 链接:https://pan.baidu.com/s/1Oz6hUB-tYWvtqfrDACM_eA 提取码:0000 打开默认是空的 通过上方的image菜单栏进行操作 导入图片 ...

  4. lua/cocos加载动画以及可以使用加载纹理的方式来替换图片并且加载个人制作的艺术字体(fnt字体)

    1.加载spine/json(ExportJson)骨骼动画 现在用的比较多 local spineAnim = sp.SkeletonAnimation:create("base/res/ ...

  5. Unity制作美术字体、图片转字体库C#

    前言:在遇到Unity支持的字体库之外的字体时,可以将每个字的图片集中生成一个字体库,用来Unity文本使用. 一.功能 1.字体图片设置 字体图片等比等分在一张图片上,方便Unity系统自处理 2. ...

  6. 【Unity】对Unity引用三方字体的踩坑日志

    一.Unity导入三方字体 导入字体文件 要在项目中添加字体,必须将字体文件放在 Assets 文件夹中.然后,Unity 会自动将其导入.支持的字体格式为 TrueType 字体(.ttf 文件)和 ...

  7. unity创建纹理_创建带纹理的文本的技术

    unity创建纹理 View demo 查看演示 Download Source 下载源 In this article we're going to explore several techniqu ...

  8. Unity教程||Unity添加中文字体||Unity知识记录--制作UI粒子特效

    Unity添加中文字体 ## 1.拷贝字体文件 拷贝C:\Windows\Fonts文件夹下,华文细黑常规文件到项目中 ## 2.下载中文字库 链接: https://pan.baidu.com/s/ ...

  9. Unity 创建2D平台游戏开发学习教程

    了解如何使用C#在Unity中创建您的第一款2D平台游戏 你会学到什么 使用Unity创建2D奥运会 使用可脚本化的对象和单一模式 使用良好的编程实践 创造武器和射弹 使用可脚本化的对象和委托模式创建 ...

  10. Unity创建使用操纵杆飞行动画教程

    Unity 3d移动超级英雄使用操纵杆飞行 MP4 |视频:h264,1280×720 |音频:AAC,44.1 KHz,2 Ch 语言:英语+中英文字幕(根据原英文字幕机译更准确) |时长:20节课 ...

最新文章

  1. hibernate分页
  2. mysql 工具_MySQL压力测试工具,值得收藏
  3. python学习四(处理数据)
  4. Tomcat映射虚拟路径到指定磁盘(eclipse)
  5. java 生成静态html的一段代码
  6. HDU 6395 Sequence(分段矩阵快速幂)题解
  7. 的唯一性_原神:被氪金玩家淹没的角色,输出很高,技能具有唯一性
  8. 【scratch案例教学】scratch端午节划龙舟比赛 scratch创意编程 少儿编程 边玩边学过个快乐端午节
  9. 程序员到底要学什么?
  10. 无人机倾斜摄影技术在不动产项目中的实际运用
  11. 如何在word左侧显示目录
  12. 使用iperf测试网速
  13. HTML+CSS+JavaScript实现放大镜效果
  14. 洛谷P4238 多项式乘法逆元
  15. 用python输入 菱形
  16. 浅谈什么是web应用防火墙(WAF)
  17. 微信开发(一)--分享接口
  18. 使用Python获取股市市场概念数据
  19. 用php获取本机的IP
  20. os的概念,基本特征

热门文章

  1. EndNote自定义引用格式和参考文献格式
  2. arXiv上引用文章在bibtex下的引用格式
  3. 检测VC++Redistributable运行库 vcredist_x86.exe
  4. 读《我的成功为什么可以复制》——唐骏
  5. 怎么从零基础学计算机打字,新手学电脑打字 0基础打字快速上手教程
  6. 算法竞赛入门经典(第二版) —— 第一章 程序设计入门
  7. PyTorch-07 卷积神经网络(什么是卷积、卷积神经网络、池化层、Batch normalization、经典卷积网络、深度残差网络 ResNet、nn.Module、数据增强)
  8. 动视暴雪利润下降22%,投身移动端能否重回王座?
  9. 工作 3 年和读研 3 年哪个更值?
  10. 基于PHP语言的汉语转拼音的类