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

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 all
                    StandardOutputEncoding = 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.kler.cn/a/280340.html

相关文章:

  • spingboot实现常规增删改查
  • 如何使用ssm实现亿互游在线平台设计与开发+vue
  • QT opencv(显示图片和视频)
  • 电脑浏览器打不开部分网页
  • ARP(地址解析协议)详解
  • mysql数据库----简单认识库的操作
  • 什么是死锁
  • 大模型从入门到精通—— LLM 应用评估(二)
  • 【C++ 面试 - 面向对象】每日 3 题(七)
  • RabbitMQ中的死信交换机?(RabbitMQ延迟队列有了解过吗)
  • 中庸就是五五开,各打五十大板吗
  • 8.28-回顾+容器与主机之间的通信+跨主机容器之间的通信
  • 如何监控Eureka集群:Prometheus与Grafana的监控集成
  • 音频分割软件有什么?最方便的音频分割软件分享给你
  • client网络模块的开发和client与server端的部分联动调试
  • FPGA工程师成长路线(持续更新ing,欢迎补充)
  • Ps:首选项 - 常规
  • golang-gin使用中间件处理文本-时间字符串格式
  • 解决 VMware 中 Ubuntu文件系统磁盘空间不足
  • 二十九、channel的select