Unity游戏FontPruner字体压缩工具

FontPruner 字体精简工具
我将FontPruner改写成Unity插件的形式,可以直接在Unity编辑器中使用
FontPrunerEditor.cs放在Editor文件夹中

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;

public class FontPrunerEditor : Editor
{
    [MenuItem ("Assets/Execute FontPruner", false, 50)]
    private static void ExecuteFontPruner ()
    {
        //第一步:检查必要的文件是否存在
        //sfnttool.jar  压缩工具
        //Custom.txt    压缩内容
        string rootPath = Application.dataPath.Replace("Assets", "");
        string fontPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
        string fontName = fontPath.Substring(fontPath.LastIndexOf("/")+1);
        string[] nameArr = fontName.Split('.');
        string targetFontName = nameArr[0] + "-min." + nameArr[1];
        string dataPath = rootPath + fontPath.Replace(fontName, "");

        string toolPath = dataPath + "sfnttool.jar";
        string textPath = dataPath + "Custom.txt";
        if(!File.Exists(toolPath))
        {
            UnityEngine.Debug.Log("缺少sfnttool.jar");
            return;
        }
        if(!File.Exists(textPath))
        {
            UnityEngine.Debug.Log("缺少Custom.txt");
            return;
        }

        //第二步:执行python脚本(需要传递参数)
        //参数1:字体文件路径
        //参数2:压缩前的字体文件名称
        //参数3:压缩后的字体文件名称
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "python";
        startInfo.Arguments = dataPath + "FontPruner.py " + dataPath + " " + fontName + " " + targetFontName;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        Process process = Process.Start(startInfo);
        process.BeginOutputReadLine();
        process.OutputDataReceived += new DataReceivedEventHandler(DataReceivedEvent);
        process.WaitForExit();
        process.Close();
        
        AssetDatabase.Refresh();
        UnityEngine.Debug.Log("字体压缩完成");
    }

    private static void DataReceivedEvent(object sender, DataReceivedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Data)) {
            UnityEngine.Debug.Log (e.Data);
        }
    }

    [MenuItem ("Assets/Execute FontPruner", true)]
    private static bool ValidateExecuteFontPruner ()
    {
        return Selection.activeObject.GetType () == typeof(Font);
    }
}

FontPruner.py放在字体文件的同一目录下

#!/bin/python

import os
import sys

#print(sys.argv)

dataPath = sys.argv[1]
sourceFontName = sys.argv[2]
targetFontName = sys.argv[3]

toolPath = dataPath + "sfnttool.jar"
customTextPath = dataPath + "Custom.txt"
sourceFontPath = dataPath + sourceFontName
targetFontPath = dataPath + targetFontName

print(targetFontPath)

command = "java -jar " + toolPath + " -c " + customTextPath + " " + sourceFontPath + " " + targetFontPath

print(command)

os.system(command)

知识点:
(1)在Unity编辑器中,通过Process执行python脚本(shell脚本)
(2)使用sfntly中的sfnttool.jar提取中文字体

FontPruner压缩字体
FontForge查看字体

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容