using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEngine.UI; using System; using System.Text.RegularExpressions; using System.Reflection; public class CheckFontSwitch { static string windowRootPath = Application.dataPath + "/ResourcesOut/UI/Window"; static string windowRootPath2 = Application.dataPath + "/ResourcesOut/UI/PriorityWindow"; static string prefabRootPath = Application.dataPath + "/ResourcesOut/UI/Prefab"; public static void CheckAndReplaceFontSwitch() { try { var allFiles = new List(); allFiles.AddRange(new DirectoryInfo(windowRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); allFiles.AddRange(new DirectoryInfo(windowRootPath2).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); allFiles.AddRange(new DirectoryInfo(prefabRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); var count = allFiles.Count; var index = 0; foreach (var file in allFiles) { var assetPath = file.FullName.Replace("\\", "/").Replace(Application.dataPath, "Assets"); var prefab = AssetDatabase.LoadAssetAtPath(assetPath); var texts = prefab.GetComponentsInChildren(true); foreach (var text in texts) { if (text.font != null) { if (text.font.name == FontUtility.preferred.name) { var fontSwitch = text.gameObject.AddMissingComponent(); fontSwitch.fontType = FontSwitch.FontType.Preferred; text.font = null; } else if (text.font.name == FontUtility.secondary.name) { var fontSwitch = text.gameObject.AddMissingComponent(); fontSwitch.fontType = FontSwitch.FontType.Secondary; text.font = null; } } } index++; EditorUtility.SetDirty(prefab); EditorUtility.DisplayProgressBar("检测和替换FontSwitch", StringUtility.Contact("正在替换:", prefab.name), (float)index / count); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } catch (Exception ex) { Debug.Log(ex); } } static List chineseCharactorErrors = new List(); public static void FindChineseCharacter() { try { chineseCharactorErrors.Clear(); chineseCharactorErrors.Add(StringUtility.Contact("序号", "\t", "预置体", "\t", "组件名", "\t", "内容", "\t", "是否需要整改")); var allFiles = new List(); allFiles.AddRange(new DirectoryInfo(windowRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); allFiles.AddRange(new DirectoryInfo(windowRootPath2).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); allFiles.AddRange(new DirectoryInfo(prefabRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly)); var inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); var count = allFiles.Count; var index = 0; foreach (var file in allFiles) { var assetPath = file.FullName.Replace("\\", "/").Replace(Application.dataPath, "Assets"); var prefab = AssetDatabase.LoadAssetAtPath(assetPath); var prefabText = File.ReadAllText(file.FullName); var texts = prefab.GetComponentsInChildren(true); foreach (var text in texts) { if (ContainChineseCharacter(text.text)) { var content = text.text.Replace("\t", "").Replace("\r\n", "").Replace("\n", "").Replace("\r", ""); var serializedObject = new SerializedObject(text); inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null); var localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile"); var fieldId = localIdProp.longValue; var serialized = ContainGuid(prefabText, fieldId.ToString()); var isFromLanguage = text is TextEx && (text as TextEx).isKey; chineseCharactorErrors.Add(StringUtility.Contact(chineseCharactorErrors.Count, "\t", prefab.name, "\t", text.name, "\t", content, "\t", !serialized && !isFromLanguage)); Debug.LogFormat("UI预置体Text配置错误-->预置体是:{0},组件名:{1},内容是:{2}", prefab, text.name, text.text); } } index++; EditorUtility.SetDirty(prefab); EditorUtility.DisplayProgressBar("查找中文字", StringUtility.Contact("正在检查:", prefab.name), (float)index / count); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } catch (Exception ex) { Debug.Log(ex); } finally { File.WriteAllLines(Application.dataPath + "/UIPrefabChineseCharacterError.txt", chineseCharactorErrors.ToArray()); } } static string chineseCharacterPattern = "[\u4e00-\u9fa5]{2,4}"; static bool ContainChineseCharacter(string _input) { if (string.IsNullOrEmpty(_input)) { return false; } else { return Regex.IsMatch(_input, chineseCharacterPattern); } } static bool ContainGuid(string _serializedFile, string _guid) { if (string.IsNullOrEmpty(_serializedFile)) { return false; } else { var matches = Regex.Matches(_serializedFile, _guid); return matches.Count > 2; } } }