ChangeTextForScene.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. public static class ChangeTextForScene
  6. {
  7. [MenuItem("切换/Scene_TextForTMP/检测场景脚本", false)]
  8. public static void CheckScripts()
  9. {
  10. StartCheckScripts();
  11. }
  12. [MenuItem("切换/Scene_TextForTMP/替换Text", false)]
  13. public static void ChangeText()
  14. {
  15. StartChangeScripts();
  16. }
  17. private static void StartChangeScripts()
  18. {
  19. var tmp_Font = Resources.Load<TMP_FontAsset>("GHZLangChaoFont");
  20. GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
  21. for (int i = 0; i < all.Length; i++)
  22. {
  23. GameObject go = all[i];
  24. if (go.GetComponent<Text>())
  25. {
  26. Text text = go.GetComponent<Text>();
  27. Transform target = text.transform;
  28. Vector2 size = text.rectTransform.sizeDelta;
  29. string strContent = text.text;
  30. Color color = text.color;
  31. int fontSize = text.fontSize;
  32. FontStyle fontStyle = text.fontStyle;
  33. TextAnchor textAnchor = text.alignment;
  34. bool richText = text.supportRichText;
  35. HorizontalWrapMode horizontalWrapMode = text.horizontalOverflow;
  36. VerticalWrapMode verticalWrapMode = text.verticalOverflow;
  37. bool raycastTarget = text.raycastTarget;
  38. Debug.LogError($"预制体名称:{go.name}");
  39. GameObject.DestroyImmediate(text);
  40. TextMeshProUGUI textMeshPro = target.gameObject.AddComponent<TextMeshProUGUI>();
  41. textMeshPro.rectTransform.sizeDelta = size;
  42. textMeshPro.text = strContent;
  43. textMeshPro.color = color;
  44. textMeshPro.font = tmp_Font;
  45. textMeshPro.fontSize = fontSize;
  46. textMeshPro.fontStyle = fontStyle == FontStyle.BoldAndItalic ? FontStyles.Bold : (FontStyles)fontStyle;
  47. switch (textAnchor)
  48. {
  49. case TextAnchor.UpperLeft:
  50. textMeshPro.alignment = TextAlignmentOptions.TopLeft;
  51. break;
  52. case TextAnchor.UpperCenter:
  53. textMeshPro.alignment = TextAlignmentOptions.Top;
  54. break;
  55. case TextAnchor.UpperRight:
  56. textMeshPro.alignment = TextAlignmentOptions.TopRight;
  57. break;
  58. case TextAnchor.MiddleLeft:
  59. textMeshPro.alignment = TextAlignmentOptions.MidlineLeft;
  60. break;
  61. case TextAnchor.MiddleCenter:
  62. textMeshPro.alignment = TextAlignmentOptions.Midline;
  63. break;
  64. case TextAnchor.MiddleRight:
  65. textMeshPro.alignment = TextAlignmentOptions.MidlineRight;
  66. break;
  67. case TextAnchor.LowerLeft:
  68. textMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  69. break;
  70. case TextAnchor.LowerCenter:
  71. textMeshPro.alignment = TextAlignmentOptions.Bottom;
  72. break;
  73. case TextAnchor.LowerRight:
  74. textMeshPro.alignment = TextAlignmentOptions.BottomRight;
  75. break;
  76. }
  77. textMeshPro.richText = richText;
  78. if (verticalWrapMode == VerticalWrapMode.Overflow)
  79. {
  80. textMeshPro.enableWordWrapping = true;
  81. textMeshPro.overflowMode = TextOverflowModes.Overflow;
  82. }
  83. else
  84. {
  85. textMeshPro.enableWordWrapping = horizontalWrapMode == HorizontalWrapMode.Overflow ? false : true;
  86. }
  87. textMeshPro.raycastTarget = raycastTarget;
  88. }
  89. }
  90. }
  91. private static void StartCheckScripts()
  92. {
  93. GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
  94. for (int i = 0; i < all.Length; i++)
  95. {
  96. var Components = all[i].GetComponentsInChildren<MonoBehaviour>();
  97. foreach (var component in Components)
  98. {
  99. if (component && !component.GetType().ToString().Contains("UnityEngine"))
  100. {
  101. //Debug.LogError("物体:"+all[i].name+"++"+component.GetType());
  102. if(all[i].GetComponentInChildren<Text>())
  103. {
  104. Debug.LogError("物体:"+all[i].name+"++"+component.GetType());
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }