123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- using UnityEngine.UI;
- using TMPro;
- public static class ChangeTextForTMP
- {
- static List<string> list = new List<string>();
- [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<TMP_FontAsset>("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<Text>();
- if (textList.Length > 0)
- {
- SaveCount++;
- GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]);
- if (root)
- {
- Text[] list = root.GetComponentsInChildren<Text>(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<TextMeshProUGUI>();
- 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<MonoBehaviour>();
- 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<TMP_FontAsset>("GHZLangChaoFont");
- for (int i = 0; i < prefabs.Length; i++)
- {
- GameObject go = AssetDatabase.LoadAssetAtPath(prefabs[i], typeof(System.Object)) as GameObject;
- Text[] textList = go.GetComponentsInChildren<Text>();
- if (textList.Length > 0)
- {
- SaveCount++;
- GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]);
- if (root)
- {
- Text[] list = root.GetComponentsInChildren<Text>(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<TextMeshProUGUI>();
- 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}个保存失败!");
- }
- }
|