using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEngine.UI; using TMPro; public static class ChangeTextForTMP { static List list = new List(); [MenuItem("切换/Asset_TextForTMP/无脚本预制体", false, 0)] public static void Change() { GetPrefabInfo(); } private static int SaveCount = 0; private static int SaveCountSuccess= 0; private static int SaveCountError = 0; private static void GetPrefabInfo() { //获取所用的预制体路径 string[] prefabs = Directory.GetFiles("Assets", "*.prefab", SearchOption.AllDirectories); var tmp_Font = Resources.Load("GHZLangChaoFont"); for (int i = 0; i < prefabs.Length; i++) { GameObject go = AssetDatabase.LoadAssetAtPath(prefabs[i], typeof(System.Object)) as GameObject; if (!CheckScript(go)) { Text[] textList = go.GetComponentsInChildren(); if (textList.Length > 0) { SaveCount++; GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]); if (root) { Text[] list = root.GetComponentsInChildren(true); for (int j = 0; j < list.Length; j++) { Text text = list[j]; 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; 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; } } PrefabUtility.SaveAsPrefabAsset(root, prefabs[i], out bool success); { if (success) SaveCountSuccess++; else { SaveCountError++; Debug.LogError($"预制体:{prefabs[i]} 保存失败!"); } } } } } Debug.LogError($"总共保存了{SaveCount}个预制体,其中{SaveCountSuccess}个保存成功,其中{SaveCountError}个保存失败!"); } private static bool CheckScript(GameObject go) { var Components = go.GetComponentsInChildren(); foreach (var component in Components) { if (component && !component.GetType().ToString().Contains("UnityEngine")) { Debug.LogError($"预制体名称:{go.name},组件:{component.GetType()}"); return true; } } return false; } [MenuItem("切换/Asset_TextForTMP/所有预制体", false, 0)] public static void ChangeALL() { SetALLPrefabInfo(); } private static void SetALLPrefabInfo() { //获取所用的预制体路径 string[] prefabs = Directory.GetFiles("Assets", "*.prefab", SearchOption.AllDirectories); var tmp_Font = Resources.Load("GHZLangChaoFont"); for (int i = 0; i < prefabs.Length; i++) { GameObject go = AssetDatabase.LoadAssetAtPath(prefabs[i], typeof(System.Object)) as GameObject; Text[] textList = go.GetComponentsInChildren(); if (textList.Length > 0) { SaveCount++; GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]); if (root) { Text[] list = root.GetComponentsInChildren(true); for (int j = 0; j < list.Length; j++) { Text text = list[j]; 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; 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; } } PrefabUtility.SaveAsPrefabAsset(root, prefabs[i], out bool success); { if (success) SaveCountSuccess++; else { SaveCountError++; Debug.LogError($"预制体:{prefabs[i]} 保存失败!"); } } } } Debug.LogError($"总共保存了{SaveCount}个预制体,其中{SaveCountSuccess}个保存成功,其中{SaveCountError}个保存失败!"); } }