123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<TMP_FontAsset>("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 text = go.GetComponent<Text>();
- 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<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;
- }
- }
- }
- 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<MonoBehaviour>();
- foreach (var component in Components)
- {
- if (component && !component.GetType().ToString().Contains("UnityEngine"))
- {
- //Debug.LogError("物体:"+all[i].name+"++"+component.GetType());
- if(all[i].GetComponentInChildren<Text>())
- {
- Debug.LogError("物体:"+all[i].name+"++"+component.GetType());
- }
- }
- }
- }
- }
- }
|