using UnityEngine; using UnityEditor; using UnityEngine.UI; using TMPro; public static class ChangeTextForScene { [MenuItem("切换/Scene_TextForTMP/检测场景脚本", false)] public static void CheckScripts() { StartCheckScripts(); } [MenuItem("切换/Scene_TextForTMP/替换Text", false)] public static void ChangeText() { StartChangeScripts(); } private static void StartChangeScripts() { var tmp_Font = Resources.Load("GHZLangChaoFont"); GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]; for (int i = 0; i < all.Length; i++) { GameObject go = all[i]; if (go.GetComponent()) { Text text = go.GetComponent(); Transform target = text.transform; Vector2 size = text.rectTransform.sizeDelta; string strContent = text.text; Color color = text.color; int fontSize = text.fontSize; FontStyle fontStyle = text.fontStyle; TextAnchor textAnchor = text.alignment; bool richText = text.supportRichText; HorizontalWrapMode horizontalWrapMode = text.horizontalOverflow; VerticalWrapMode verticalWrapMode = text.verticalOverflow; bool raycastTarget = text.raycastTarget; Debug.LogError($"预制体名称:{go.name}"); GameObject.DestroyImmediate(text); TextMeshProUGUI textMeshPro = target.gameObject.AddComponent(); textMeshPro.rectTransform.sizeDelta = size; textMeshPro.text = strContent; textMeshPro.color = color; textMeshPro.font = tmp_Font; textMeshPro.fontSize = fontSize; textMeshPro.fontStyle = fontStyle == FontStyle.BoldAndItalic ? FontStyles.Bold : (FontStyles)fontStyle; switch (textAnchor) { case TextAnchor.UpperLeft: textMeshPro.alignment = TextAlignmentOptions.TopLeft; break; case TextAnchor.UpperCenter: textMeshPro.alignment = TextAlignmentOptions.Top; break; case TextAnchor.UpperRight: textMeshPro.alignment = TextAlignmentOptions.TopRight; break; case TextAnchor.MiddleLeft: textMeshPro.alignment = TextAlignmentOptions.MidlineLeft; break; case TextAnchor.MiddleCenter: textMeshPro.alignment = TextAlignmentOptions.Midline; break; case TextAnchor.MiddleRight: textMeshPro.alignment = TextAlignmentOptions.MidlineRight; break; case TextAnchor.LowerLeft: textMeshPro.alignment = TextAlignmentOptions.BottomLeft; break; case TextAnchor.LowerCenter: textMeshPro.alignment = TextAlignmentOptions.Bottom; break; case TextAnchor.LowerRight: textMeshPro.alignment = TextAlignmentOptions.BottomRight; break; } textMeshPro.richText = richText; if (verticalWrapMode == VerticalWrapMode.Overflow) { textMeshPro.enableWordWrapping = true; textMeshPro.overflowMode = TextOverflowModes.Overflow; } else { textMeshPro.enableWordWrapping = horizontalWrapMode == HorizontalWrapMode.Overflow ? false : true; } textMeshPro.raycastTarget = raycastTarget; } } } private static void StartCheckScripts() { GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]; for (int i = 0; i < all.Length; i++) { var Components = all[i].GetComponentsInChildren(); foreach (var component in Components) { if (component && !component.GetType().ToString().Contains("UnityEngine")) { //Debug.LogError("物体:"+all[i].name+"++"+component.GetType()); if(all[i].GetComponentInChildren()) { Debug.LogError("物体:"+all[i].name+"++"+component.GetType()); } } } } } }