当前位置: 首页 > news >正文

Unity项目增加字体裁剪

因为项目里有字体裁剪缩小字体文件的需求,在网上搜索了一番。
有个很靠谱的参考文章:
https://www.cnblogs.com/yaukey/p/compare_fontsubsetgui_fontpruner_for_unity.html
然后就使用了这篇文章里提到的FontPruner工具。
下载之后就是文章置顶附件这样的jar包,放入工程里。

需要编写脚本,根据表格工具导出的项目文本对应字体的txt文件,再使用裁剪工具进行裁剪。

private static void ExecuteFontPruner(string fontName){UnityEngine.Debug.Log($"[FontCreateTool]:开始重新生成 [{fontName}] 字体");var txtPath = $"{fontToolPath}/{fontName}.txt";var fontFullPath = Application.dataPath + $"{fontOriginResPath}/{fontName}.ttf";fontFullPath = fontFullPath.Replace("Assets/", "");var newFontPath = $"{resourcePath}/{fontName}.bytes";if (!File.Exists(txtPath)){UnityEngine.Debug.LogError($"[FontCreateTool]:{txtPath} 路径不存在");return;}if (!File.Exists(fontFullPath)){UnityEngine.Debug.LogError($"[FontCreateTool]:{fontFullPath} 路径不存在");return;}var workingPath = GetFullPath(fontPrunerPath);var fileName = "java";//java -jar sfnttool.jar -s '需要提取的字体' 源字体库 导出的最终字体库var commandLine = " -jar bin/sfnttool.jar -c " +GetFullPath(txtPath) + " " +fontFullPath + " " +GetFullPath(newFontPath);UnityEngine.Debug.Log($"[FontCreateTool]:[FontPruner-DoProcess]commandLine: {commandLine}");Process process = Process.Start(new ProcessStartInfo(fileName, commandLine){RedirectStandardError = true,RedirectStandardOutput = true,UseShellExecute = false,CreateNoWindow = true,WorkingDirectory = workingPath,// http://stackoverflow.com/questions/16803748/how-to-decode-cmd-output-correctly// Default = 65533, ASCII = ?, Unicode = nothing works at all, UTF-8 = 65533, UTF-7 = 242 = WORKS!, UTF-32 = nothing works at allStandardOutputEncoding = Encoding.GetEncoding(850)});if (!process.WaitForExit(timeOut)){UnityEngine.Debug.LogWarning("[FontCreateTool]: FontPruner took too long to finish.  Killing operation.");process.Kill();}string error = process.StandardError.ReadToEnd();if (!string.IsNullOrEmpty(error)){UnityEngine.Debug.LogError(error);}string output = process.StandardOutput.ReadToEnd();if (!string.IsNullOrEmpty(output)){UnityEngine.Debug.Log(output);}AssetDatabase.Refresh();UnityEngine.Debug.Log("[FontCreateTool]:字体裁剪完成");}

实际使用中,有些字体在裁剪的过程中会碰到报错:

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index attempted to be read from is out of bounds: a34c

实际发现是因为字体库里有很多没定义的字形,使用FontCreator工具编辑ttf,把没用的全删除掉之后再调用裁剪就不报错了。

再附一个【FontCreator注册码】网站,毕竟我找了半天……之前免费用一段时间就卸注册表,太麻烦了。
https://www.huajclub.com/8068.html


http://www.mrgr.cn/news/12273.html

相关文章:

  • git 拉取分支
  • HarmonyOS开发之Gauge(环形图表)的简单使用
  • Python | Leetcode Python题解之第373题查找和最小的K对数字
  • 【iOS】Masonry学习
  • 前端实习手记(9):修修修修bug
  • 二叉树的三个简单题
  • 速盾:cdn可以解决带宽问题么
  • GalaChain 全面剖析:为 Web3 游戏和娱乐而生的创新区块链
  • QT中通过Tcp协议的多线程的文件传输(服务器)
  • 3 Docker 镜像推送
  • 鸿蒙验证码,鸿蒙认证服务验证码,鸿蒙云存储上传图片
  • 华裔二、三代长相变迁的多维度解析
  • 利用深度学习技术来实现街景图像的语义分割(街景图像语义分割)
  • 如何使用 Nginx 解决跨域问题 (CORS)
  • uni-app - - - - - 自定义tabbar
  • 使用 OpenCV 组合和缩放多张图像
  • DDPM/DDIM去噪扩散概率模型和GANs
  • 查看redis节点的连接数
  • 多态(c++)
  • pytorch深度学习基础 8(CIFRA-10基础篇1)