ChangeTextForTMP.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. public static class ChangeTextForTMP
  8. {
  9. static List<string> list = new List<string>();
  10. [MenuItem("切换/Asset_TextForTMP/无脚本预制体", false, 0)]
  11. public static void Change()
  12. {
  13. GetPrefabInfo();
  14. }
  15. private static int SaveCount = 0;
  16. private static int SaveCountSuccess= 0;
  17. private static int SaveCountError = 0;
  18. private static void GetPrefabInfo()
  19. {
  20. //获取所用的预制体路径
  21. string[] prefabs = Directory.GetFiles("Assets", "*.prefab", SearchOption.AllDirectories);
  22. var tmp_Font = Resources.Load<TMP_FontAsset>("GHZLangChaoFont");
  23. for (int i = 0; i < prefabs.Length; i++)
  24. {
  25. GameObject go = AssetDatabase.LoadAssetAtPath(prefabs[i], typeof(System.Object)) as GameObject;
  26. if (!CheckScript(go))
  27. {
  28. Text[] textList = go.GetComponentsInChildren<Text>();
  29. if (textList.Length > 0)
  30. {
  31. SaveCount++;
  32. GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]);
  33. if (root)
  34. {
  35. Text[] list = root.GetComponentsInChildren<Text>(true);
  36. for (int j = 0; j < list.Length; j++)
  37. {
  38. Text text = list[j];
  39. Transform target = text.transform;
  40. Vector2 size = text.rectTransform.sizeDelta;
  41. string strContent = text.text;
  42. Color color = text.color;
  43. int fontSize = text.fontSize;
  44. FontStyle fontStyle = text.fontStyle;
  45. TextAnchor textAnchor = text.alignment;
  46. bool richText = text.supportRichText;
  47. HorizontalWrapMode horizontalWrapMode = text.horizontalOverflow;
  48. VerticalWrapMode verticalWrapMode = text.verticalOverflow;
  49. bool raycastTarget = text.raycastTarget;
  50. GameObject.DestroyImmediate(text);
  51. TextMeshProUGUI textMeshPro = target.gameObject.AddComponent<TextMeshProUGUI>();
  52. textMeshPro.rectTransform.sizeDelta = size;
  53. textMeshPro.text = strContent;
  54. textMeshPro.color = color;
  55. textMeshPro.font = tmp_Font;
  56. textMeshPro.fontSize = fontSize;
  57. textMeshPro.fontStyle = fontStyle == FontStyle.BoldAndItalic ? FontStyles.Bold : (FontStyles)fontStyle;
  58. switch (textAnchor)
  59. {
  60. case TextAnchor.UpperLeft:
  61. textMeshPro.alignment = TextAlignmentOptions.TopLeft;
  62. break;
  63. case TextAnchor.UpperCenter:
  64. textMeshPro.alignment = TextAlignmentOptions.Top;
  65. break;
  66. case TextAnchor.UpperRight:
  67. textMeshPro.alignment = TextAlignmentOptions.TopRight;
  68. break;
  69. case TextAnchor.MiddleLeft:
  70. textMeshPro.alignment = TextAlignmentOptions.MidlineLeft;
  71. break;
  72. case TextAnchor.MiddleCenter:
  73. textMeshPro.alignment = TextAlignmentOptions.Midline;
  74. break;
  75. case TextAnchor.MiddleRight:
  76. textMeshPro.alignment = TextAlignmentOptions.MidlineRight;
  77. break;
  78. case TextAnchor.LowerLeft:
  79. textMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  80. break;
  81. case TextAnchor.LowerCenter:
  82. textMeshPro.alignment = TextAlignmentOptions.Bottom;
  83. break;
  84. case TextAnchor.LowerRight:
  85. textMeshPro.alignment = TextAlignmentOptions.BottomRight;
  86. break;
  87. }
  88. textMeshPro.richText = richText;
  89. if (verticalWrapMode == VerticalWrapMode.Overflow)
  90. {
  91. textMeshPro.enableWordWrapping = true;
  92. textMeshPro.overflowMode = TextOverflowModes.Overflow;
  93. }
  94. else
  95. {
  96. textMeshPro.enableWordWrapping = horizontalWrapMode == HorizontalWrapMode.Overflow ? false : true;
  97. }
  98. textMeshPro.raycastTarget = raycastTarget;
  99. }
  100. }
  101. PrefabUtility.SaveAsPrefabAsset(root, prefabs[i], out bool success);
  102. {
  103. if (success)
  104. SaveCountSuccess++;
  105. else
  106. {
  107. SaveCountError++;
  108. Debug.LogError($"预制体:{prefabs[i]} 保存失败!");
  109. }
  110. }
  111. }
  112. }
  113. }
  114. Debug.LogError($"总共保存了{SaveCount}个预制体,其中{SaveCountSuccess}个保存成功,其中{SaveCountError}个保存失败!");
  115. }
  116. private static bool CheckScript(GameObject go)
  117. {
  118. var Components = go.GetComponentsInChildren<MonoBehaviour>();
  119. foreach (var component in Components)
  120. {
  121. if (component && !component.GetType().ToString().Contains("UnityEngine"))
  122. {
  123. Debug.LogError($"预制体名称:{go.name},组件:{component.GetType()}");
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. [MenuItem("切换/Asset_TextForTMP/所有预制体", false, 0)]
  130. public static void ChangeALL()
  131. {
  132. SetALLPrefabInfo();
  133. }
  134. private static void SetALLPrefabInfo()
  135. {
  136. //获取所用的预制体路径
  137. string[] prefabs = Directory.GetFiles("Assets", "*.prefab", SearchOption.AllDirectories);
  138. var tmp_Font = Resources.Load<TMP_FontAsset>("GHZLangChaoFont");
  139. for (int i = 0; i < prefabs.Length; i++)
  140. {
  141. GameObject go = AssetDatabase.LoadAssetAtPath(prefabs[i], typeof(System.Object)) as GameObject;
  142. Text[] textList = go.GetComponentsInChildren<Text>();
  143. if (textList.Length > 0)
  144. {
  145. SaveCount++;
  146. GameObject root = PrefabUtility.LoadPrefabContents(prefabs[i]);
  147. if (root)
  148. {
  149. Text[] list = root.GetComponentsInChildren<Text>(true);
  150. for (int j = 0; j < list.Length; j++)
  151. {
  152. Text text = list[j];
  153. Transform target = text.transform;
  154. Vector2 size = text.rectTransform.sizeDelta;
  155. string strContent = text.text;
  156. Color color = text.color;
  157. int fontSize = text.fontSize;
  158. FontStyle fontStyle = text.fontStyle;
  159. TextAnchor textAnchor = text.alignment;
  160. bool richText = text.supportRichText;
  161. HorizontalWrapMode horizontalWrapMode = text.horizontalOverflow;
  162. VerticalWrapMode verticalWrapMode = text.verticalOverflow;
  163. bool raycastTarget = text.raycastTarget;
  164. GameObject.DestroyImmediate(text);
  165. TextMeshProUGUI textMeshPro = target.gameObject.AddComponent<TextMeshProUGUI>();
  166. textMeshPro.rectTransform.sizeDelta = size;
  167. textMeshPro.text = strContent;
  168. textMeshPro.color = color;
  169. textMeshPro.font = tmp_Font;
  170. textMeshPro.fontSize = fontSize;
  171. textMeshPro.fontStyle = fontStyle == FontStyle.BoldAndItalic ? FontStyles.Bold : (FontStyles)fontStyle;
  172. switch (textAnchor)
  173. {
  174. case TextAnchor.UpperLeft:
  175. textMeshPro.alignment = TextAlignmentOptions.TopLeft;
  176. break;
  177. case TextAnchor.UpperCenter:
  178. textMeshPro.alignment = TextAlignmentOptions.Top;
  179. break;
  180. case TextAnchor.UpperRight:
  181. textMeshPro.alignment = TextAlignmentOptions.TopRight;
  182. break;
  183. case TextAnchor.MiddleLeft:
  184. textMeshPro.alignment = TextAlignmentOptions.MidlineLeft;
  185. break;
  186. case TextAnchor.MiddleCenter:
  187. textMeshPro.alignment = TextAlignmentOptions.Midline;
  188. break;
  189. case TextAnchor.MiddleRight:
  190. textMeshPro.alignment = TextAlignmentOptions.MidlineRight;
  191. break;
  192. case TextAnchor.LowerLeft:
  193. textMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  194. break;
  195. case TextAnchor.LowerCenter:
  196. textMeshPro.alignment = TextAlignmentOptions.Bottom;
  197. break;
  198. case TextAnchor.LowerRight:
  199. textMeshPro.alignment = TextAlignmentOptions.BottomRight;
  200. break;
  201. }
  202. textMeshPro.richText = richText;
  203. if (verticalWrapMode == VerticalWrapMode.Overflow)
  204. {
  205. textMeshPro.enableWordWrapping = true;
  206. textMeshPro.overflowMode = TextOverflowModes.Overflow;
  207. }
  208. else
  209. {
  210. textMeshPro.enableWordWrapping = horizontalWrapMode == HorizontalWrapMode.Overflow ? false : true;
  211. }
  212. textMeshPro.raycastTarget = raycastTarget;
  213. }
  214. }
  215. PrefabUtility.SaveAsPrefabAsset(root, prefabs[i], out bool success);
  216. {
  217. if (success)
  218. SaveCountSuccess++;
  219. else
  220. {
  221. SaveCountError++;
  222. Debug.LogError($"预制体:{prefabs[i]} 保存失败!");
  223. }
  224. }
  225. }
  226. }
  227. Debug.LogError($"总共保存了{SaveCount}个预制体,其中{SaveCountSuccess}个保存成功,其中{SaveCountError}个保存失败!");
  228. }
  229. }