94 lines
2.6 KiB
C#
94 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using System.Linq;
|
|
using Logic.Multilingual;
|
|
|
|
public class ChineseCharacterOptimizer : EditorWindow
|
|
{
|
|
private HashSet<char> characterSet = new HashSet<char>();
|
|
|
|
[MenuItem("Tools/中文字符优化工具")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<ChineseCharacterOptimizer>("中文字符优化");
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (GUILayout.Button("导出字符集"))
|
|
{
|
|
AddBasicCharacters();
|
|
ExtractChineseFromI2Languages();
|
|
ExportCharacterSet();
|
|
}
|
|
}
|
|
|
|
private void AddBasicCharacters()
|
|
{
|
|
// 添加大写字母
|
|
for (char c = 'A'; c <= 'Z'; c++)
|
|
{
|
|
characterSet.Add(c);
|
|
}
|
|
|
|
// 添加小写字母
|
|
for (char c = 'a'; c <= 'z'; c++)
|
|
{
|
|
characterSet.Add(c);
|
|
}
|
|
|
|
// 添加数字
|
|
for (char c = '0'; c <= '9'; c++)
|
|
{
|
|
characterSet.Add(c);
|
|
}
|
|
|
|
// 添加常用标点符号
|
|
string punctuations = ",.!?;:\"'()[]{}+-*/=_<>@#$%^&|\\~|/\~{}[]【】「」『』《》〈〉:;“” ‘’",。?!…—--_+-=×÷";
|
|
foreach (char c in punctuations)
|
|
{
|
|
characterSet.Add(c);
|
|
}
|
|
}
|
|
|
|
private void ExtractChineseFromI2Languages()
|
|
{
|
|
var path = $"Assets/Resources/Export/Multilingual.asset";
|
|
var asset = AssetDatabase.LoadAssetAtPath<MultilingualData>(path);
|
|
if (asset == null)
|
|
{
|
|
EditorUtility.DisplayDialog("错误", "未找到多语言资源文件,请确认路径是否正确。", "确定");
|
|
return;
|
|
}
|
|
|
|
foreach (var item in asset.Items)
|
|
{
|
|
foreach (var c in item.ZH)
|
|
{
|
|
if (!IsChinese(c)) continue;
|
|
characterSet.Add(c);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool IsChinese(char c)
|
|
{
|
|
return (c >= 0x4E00 && c <= 0x9FFF) || // CJK统一汉字
|
|
(c >= 0x3400 && c <= 0x4DBF) || // CJK扩展A
|
|
(c >= 0x20000 && c <= 0x2A6DF); // CJK扩展B
|
|
}
|
|
|
|
private void ExportCharacterSet()
|
|
{
|
|
string path = EditorUtility.SaveFilePanel("保存字符集", "Assets/Fonts", "ChineseCharSet", "txt");
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
File.WriteAllText(path, string.Join("", characterSet.OrderBy(c => c)), Encoding.UTF8);
|
|
EditorUtility.DisplayDialog("成功", "字符集已导出!", "确定");
|
|
}
|
|
}
|
|
} |