界面需要做国际化的时候,一般不会在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()}"); } } }
标签:prefabAssetPath,string,Text,static,体中,go,using,ugui From: https://www.cnblogs.com/sailJs/p/16668587.html