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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值