检测ugui预制体中Text上的中文
界面需要做国际化的时候,一般不会在Text组件上使用中文,而是使用id或者key来代替,这个代码用于检测直接写了中文的Text
#if UNITY_EDITOR using System.Text; using System.Text.RegularExpressions; using UnityEditor; using UnityEditor.Experimental.SceneManagement; using UnityEngine; using UnityEngine.UI; [InitializeOnLoad] public class PrefabCnTextCheck { static PrefabCnTextCheck() { PrefabStage.prefabStageOpened += OnPrefabStageOpened; PrefabStage.prefabSaved += OnPrefabStageSaved; PrefabUtility.prefabInstanceUpdated += OnPrefabUpdate; } static void OnPrefabUpdate(GameObject go) { _prefabAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go); Debug.Log($"OnPrefabUpdate: {_prefabAssetPath}"); CheckHaveCnText(go); } static string _prefabAssetPath; static void OnPrefabStageOpened(PrefabStage prefabStage) { _prefabAssetPath = prefabStage.prefabAssetPath; Debug.Log($"OnPrefabStageOpened: {_prefabAssetPath}"); } static void OnPrefabStageSaved(GameObject gameObject) { if (string.IsNullOrEmpty(_prefabAssetPath)) return; //Debug.Log($"OnPrefabStageSaved: {_prefabAssetPath}"); //CheckHaveCnText(gameObject); } static void CheckHaveCnText(GameObject go) { Regex reg = new Regex(@"[\u4e00-\u9fa5]"); Text[] texts = go.GetComponentsInChildren<Text>(true); var errorTextBuilder = new StringBuilder(); for (int i = 0; i < texts.Length; i++) { Text txt = texts[i]; string text = txt.text; if (!string.IsNullOrEmpty(text)) { if (reg.IsMatch(text)) { string path = txt.name; var prefabTransform = go.transform; var tempTransform = txt.transform; while (tempTransform != prefabTransform) { tempTransform = tempTransform.parent; path = $"{tempTransform.name}/{path}"; } errorTextBuilder.AppendLine(path); } } } if (errorTextBuilder.Length > 0) { Debug.LogError($"检测到中文: {errorTextBuilder.ToString()}"); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~