unity检查预设上的中文

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using TMPro;
using System.IO;
using System;

public class UiPrefabs 
{




    [MenuItem("Tools/检查预设中文并且生成路径")]
    static void CheckChinesePrefabsAndSerialization()
    {
        string[] prefabPaths = GetAllPrefabs("Assets/Prefab");

        if (prefabPaths == null)
        {
            return;
        }

        List<string> text = new List<string>();

        for (int i = 0; i < prefabPaths.Length; i++)
        {
            string prefabPath = prefabPaths[i];
            GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);

            if (prefab == null)
            {
                continue;
            }

            TextMeshProUGUI[] uiLabels = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);

            for (int j = 0; j < uiLabels.Length; j++)
            {
                TextMeshProUGUI uiLabel = uiLabels[j];

                if (IsIncludeChinese(uiLabel.text))
                {
                    Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
                    text.Add(uiLabel.text);
                }
            }

            //进度条
            float progressBar = (float)i / prefabPaths.Length;
            EditorUtility.DisplayProgressBar("检查预设中文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
        }


        EditorUtility.ClearProgressBar();

        AssetDatabase.Refresh();

        Debug.Log("完成检查预设中文并且生成路径");
    }


    //string path = "Assets/UI/Prefab";
    static string[] GetAllPrefabs(string directory)
    {
        if (string.IsNullOrEmpty(directory) || !directory.StartsWith("Assets"))
            throw new ArgumentException("folderPath");

        string[] subFolders = Directory.GetDirectories(directory);
        string[] guids = null;
        string[] assetPaths = null;
        int i = 0, iMax = 0;
        foreach (var folder in subFolders)
        {
            guids = AssetDatabase.FindAssets("t:Prefab", new string[] { folder });
            assetPaths = new string[guids.Length];
            for (i = 0, iMax = assetPaths.Length; i < iMax; ++i)
            {
                assetPaths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
            }
        }
        return assetPaths;
    }


    public static bool IsIncludeChinese(string content)
    {
        string regexstr = @"[\u4e00-\u9fa5]";

        if (Regex.IsMatch(content, regexstr))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_41995872/article/details/122583843