Unity编辑器工具简体繁体一键转换

项目上线难免有很多的版本,比如要上港台新马版本时就会涉及到将项目的中的文字转换为繁体中文。

制作一个一键转化工具,将文本预制体中的中文都转化为繁体中文岂美哉。

说干就干。

public class ChineseUtility: Editor
{
    [MenuItem("other/简繁装换")]
    public static void ChineseUtils()
    {
        string findPath = EditorUtility.OpenFolderPanel("Find Folder", "Resources", "");
        if (string.IsNullOrEmpty(findPath))
        {
            return;
        }
        StringBuilder builder = new StringBuilder();
        List<string> withoutExtensions = new List<string>() { ".prefab", ".txt" };
        builder.Append("修改:-----------------------\n");
        int count = 0;
        string[] files = Directory.GetFiles(findPath, "*.*", SearchOption.AllDirectories).Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];
            if (Path.GetExtension(path) == ".txt")
            {
                File.WriteAllText(files[i], ChineseStringUtility.ToTraditional(File.ReadAllText(path)));
                builder.Append(path);
                builder.Append("\n");
                count++;
            }
            else
            {
                path = GetRelativeAssetsPath(path);
                GameObject tmp = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
                Text[] labels = (tmp.GetComponentsInChildren<Text>(true));
                bool isDirty = false;
                for (int j = 0; j < labels.Length; j++)
                {
                    if (!string.IsNullOrEmpty(labels[j].text))
                    {
                        labels[j].text = ChineseStringUtility.ToTraditional(labels[j].text);
                        builder.Append(path + ":" + labels[j].name + "-" + labels[j].text);
                        builder.Append("\n");
                        isDirty = true;
                        count++;
                    }
                }
                if (isDirty)
                    EditorUtility.SetDirty(tmp);
            }

        }
        Debug.Log("总共修改:" + count + "\n" + builder.ToString());
    }
    static private string GetRelativeAssetsPath(string path)
    {
        return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
    }
}
/// <summary>
/// 中文字符工具类
/// </summary>
public static class ChineseStringUtility
{
    private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
    private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
    private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

    [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);

    /// <summary>
    /// 讲字符转换为繁体中文
    /// </summary>
    /// <param name="source">输入要转换的字符串</param>
    /// <returns>转换完成后的字符串</returns>
    public static string ToTraditional(string source)
    {
        String target = new String(' ', source.Length);
        int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
        return target;
    }
}

如果是使用的NGUI将代码中的Text换为UILabel即可。

如果你需要判断是否文本中是否有中文,可以使用如下代码:

    public static bool ContainsChinese(string text)
    {
        return  Regex.IsMatch(text, @"[\u4e00-\u9fa5]");

    }

不介意的话,可以请博主喝杯咖啡吗?
这里写图片描述这里写图片描述

猜你喜欢

转载自blog.csdn.net/qwsx789/article/details/78867581