{"id":6333,"date":"2026-03-09T19:00:09","date_gmt":"2026-03-09T11:00:09","guid":{"rendered":"https:\/\/blog.coolcoding.cn\/?p=6333"},"modified":"2026-03-09T19:01:04","modified_gmt":"2026-03-09T11:01:04","slug":"unity%e5%bc%80%e5%8f%91%e5%be%ae%e4%bf%a1%e5%b0%8f%e6%b8%b8%e6%88%8f-%e7%94%9f%e5%83%bb%e5%ad%97%e7%9a%84%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=6333","title":{"rendered":"Unity\u5f00\u53d1\u5fae\u4fe1\u5c0f\u6e38\u620f-\u751f\u50fb\u5b57\u7684\u5904\u7406"},"content":{"rendered":"\n<p>Unity\u4f7f\u7528GB2312\u5b57\u4f53\uff0c\u5728App\u4e2d\u751f\u50fb\u5b57\u4f1aFallback\u5230\u7cfb\u7edf\u5b57\u4f53\uff0c\u4f46\u662f\u5728\u5fae\u4fe1\u5c0f\u6e38\u620f\u4e2d\u76f4\u63a5\u65e0\u6cd5\u663e\u793a<\/p>\n\n\n\n<p>\u5904\u7406\u65b9\u6848\u4e3a\uff0c\u4f7f\u7528fontforge\u81ea\u52a8\u5316\u5bfc\u5165\u5176\u5b83\u66ff\u4ee3\u5b57\u4f53\u4e2d\u7684\u5b57\u5f62\u5230\u5df2\u6709\u5b57\u4f53\u4e2d\u8fdb\u884c\u8865\u5145<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1\u3001\u5b89\u88c5 fontforge<\/h3>\n\n\n\n<p>fontforge\u652f\u6301python\u811a\u672c\u81ea\u52a8\u5316\u8fd0\u884c\uff1b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2\u3001\u6dfb\u52a0\u66ff\u6362\u811a\u672creplace.py<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import fontforge\nimport os\nimport tempfile\n\ndef get_glyph_by_unicode(font, unicode_code):\n    try:\n        if unicode_code in font:\n            return font&#91;unicode_code]\n        glyph_name = f\"uni{hex(unicode_code)&#91;2:].upper()}\"  \n        if glyph_name in font:\n            return font&#91;glyph_name]\n        glyph_name_u = f\"u{hex(unicode_code)&#91;2:]}\"\n        if glyph_name_u in font:\n            return font&#91;glyph_name_u]\n        return None\n    except:\n        return None\n\ndef replace_single_glyph(source_font, target_font, unicode_code, glyph_alias=None):\n    glyph_alias = glyph_alias or f\"U+{hex(unicode_code)&#91;2:].upper()}\"\n    source_glyph = get_glyph_by_unicode(source_font, unicode_code)\n    if not source_glyph:\n        print(f\"\u8b66\u544a\uff1a\u6e90\u5b57\u4f53\u4e2d\u672a\u627e\u5230\u5b57\u5f62 {glyph_alias}\uff08Unicode\uff1a{unicode_code}\uff09\uff0c\u8df3\u8fc7\")\n        return False\n    target_glyph = get_glyph_by_unicode(target_font, unicode_code)\n    if target_glyph:\n        print(f\"\u63d0\u793a\uff1a{glyph_alias} \u5b58\u5728..\")\n        return True\n    target_font.createChar(int(unicode_code), f\"uni{hex(unicode_code)&#91;2:].upper()}\")\n    target_glyph = get_glyph_by_unicode(target_font, unicode_code)\n    if not target_glyph:\n        print(f\"\u9519\u8bef\uff1a\u65e0\u6cd5\u521b\u5efa\u5b57\u5f62 {glyph_alias}\uff0c\u8df3\u8fc7\")\n        return False\n    try:\n        source_font.selection.select(int(unicode_code))\n        source_font.copy()\n        target_font.selection.select(int(unicode_code))\n        target_font.paste()\n        target_glyph.width = int(round(source_glyph.width))\n        if hasattr(source_glyph, 'lsb'):\n            target_glyph.lsb = int(round(source_glyph.lsb))\n            target_glyph.rsb = int(round(source_glyph.rsb))\n        elif hasattr(source_glyph, 'left_side_bearing'):\n            target_glyph.left_side_bearing = int(round(source_glyph.left_side_bearing))\n            target_glyph.right_side_bearing = int(round(source_glyph.right_side_bearing))\n        \n        print(f\"\u6210\u529f\u66ff\u6362\u5b57\u5f62 {glyph_alias}\uff08Unicode\uff1a{unicode_code}\uff09\")\n        return True\n    except Exception as e:\n        print(f\"\u9519\u8bef\uff1a\u66ff\u6362\u5b57\u5f62 {glyph_alias} \u5931\u8d25 - {str(e)}\")\n        return False\n\ndef replace_glyphs(source_font_path, target_font_path, output_font_path, glyph_unicode_map):\n    temp_font = tempfile.mktemp(suffix=\".sfd\")\n    try:\n        source_font = fontforge.open(source_font_path)\n        target_font = fontforge.open(target_font_path)\n        source_em = int(round(source_font.em))\n        target_font.em = source_em\n        for glyph_alias, unicode_code in glyph_unicode_map.items():\n            replace_single_glyph(source_font, target_font, unicode_code, glyph_alias)\n        target_font.generate(output_font_path)\n        print(f\"\\n\u5b57\u4f53\u66ff\u6362\u5b8c\u6210\uff01\u65b0\u5b57\u4f53\u8def\u5f84\uff1a{output_font_path}\")\n    except Exception as e:\n        print(f\"\\n\u6267\u884c\u51fa\u9519\uff1a{str(e)}\")\n        import traceback\n        traceback.print_exc()\n    finally:\n        if 'source_font' in locals():\n            source_font.close()\n        if 'target_font' in locals():\n            target_font.close()\n        if os.path.exists(temp_font):\n            try:\n                os.remove(temp_font)\n            except:\n                pass\n\nif __name__ == \"__main__\":\n    SOURCE_FONT = \"gbk.ttf\"\n    TARGET_FONT = \"gb2312.ttf\"\n    OUTPUT_FONT = \"out.TTF\"\n    GLYPH_UNICODE_MAP = {\n\"\u777a\": 30586,\n\"\u5570\": 21872,\n\"\u59ba\": 22970,\n\"\u5dbd\": 23997,\n\"\u7328\": 29480,\n\"\u81dc\": 33244,\n\"\u96d8\": 38616,\n    }\n\n    replace_glyphs(SOURCE_FONT, TARGET_FONT, OUTPUT_FONT, GLYPH_UNICODE_MAP)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3\u3001\u4e66\u5199\u4e00\u4e2a\u5de5\u5177\u5168\u5c40\u626b\u63cf\u6240\u6709\u7684\u751f\u50fb\u5b57<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>internal class Program\n{\n\tstatic void Main(string&#91;] args)\n\t{\n\t\ttry\n\t\t{\n\t\t\tstring filePath = \"language.txt\";\n\t\t\tif (!File.Exists(filePath))\n\t\t\t{\n\t\t\t\tConsole.WriteLine($\"\u6587\u4ef6 {filePath} \u4e0d\u5b58\u5728\uff01\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstring&#91;] lines = File.ReadAllLines(filePath);\n\t\t\tHashSet&lt;char> allChars = new HashSet&lt;char>();\n\t\t\tExtractChineseCharacters(lines, allChars);\n\t\t\tSaveResults(allChars);\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tConsole.WriteLine($\"\u53d1\u751f\u9519\u8bef: {ex.Message}\");\n\t\t}\n\t}\n\tstatic void ExtractChineseCharacters(string&#91;] lines, HashSet&lt;char> chars)\n\t{\n\t\tforeach (string line in lines)\n\t\t{\n\t\t\tforeach (char c in line)\n\t\t\t{\n\t\t\t\tif (c &lt; 10000) \/\/ \u5c0f\u4e8e10000\u7684\u5b57\u7b26\u901a\u5e38\u4e0d\u662f\u6c49\u5b57\uff0c\u76f4\u63a5\u8df3\u8fc7\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (chars.Contains(c))\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (IsGB2312(c))\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tchars.Add(c);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static Encoding gb2312 = Encoding.GetEncoding(\"gb2312\");\n\tprivate static char&#91;] charBuff = new char&#91;1];\n\tpublic static bool IsGB2312(char c)\n\t{\n\t\ttry\n\t\t{\n\t\t\tcharBuff&#91;0] = c;\n\t\t\tbyte&#91;] bytes = gb2312.GetBytes(charBuff);\n\t\t\tif (bytes.Length == 2)\n\t\t\t{\n\t\t\t\tif (bytes&#91;0] >= 0xA1 &amp;&amp; bytes&#91;0] &lt;= 0xFE &amp;&amp; bytes&#91;1] >= 0xA1 &amp;&amp; bytes&#91;1] &lt;= 0xFE)\n\t\t\t\t{\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t\tcatch\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\tstatic void SaveResults(HashSet&lt;char> allChars)\n\t{\n\t\tusing (StreamWriter writer = new StreamWriter(\"GBK.txt\"))\n\t\t{\n\t\t\tforeach (char c in allChars)\n\t\t\t{\n\t\t\t\twriter.WriteLine($\"\\\"{c}\\\": {(int)c},\");\n\t\t\t}\n\t\t}\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>\u5c06\u8f93\u51fa\u7684\u751f\u50fb\u5b57\u8d34\u5230python\u811a\u672c\u4e2d\uff0c\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fontforge.exe -script replace.py<\/code><\/pre>\n\n\n\n<p>\u5373\u53ef\u83b7\u5f97\u8865\u5168\u7684\u5b57\u4f53\u6587\u4ef6out.ttf<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity\u4f7f\u7528GB2312\u5b57\u4f53\uff0c\u5728App\u4e2d\u751f\u50fb\u5b57\u4f1aFallback\u5230\u7cfb\u7edf\u5b57\u4f53\uff0c\u4f46\u662f\u5728\u5fae\u4fe1\u5c0f\u6e38\u620f\u4e2d\u76f4\u63a5\u65e0\u6cd5\u663e\u793a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6333"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6333"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6333\/revisions"}],"predecessor-version":[{"id":6335,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6333\/revisions\/6335"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}