SelectTextPrefabsWindow.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using TMPro;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class Text2TextMeshProUtil : EditorWindow
  8. {
  9. Vector2 scrollPos = Vector2.zero;
  10. static List<string> scriptsFolders;
  11. string path = "Assets";
  12. string Path
  13. {
  14. get
  15. {
  16. return path;
  17. }
  18. set
  19. {
  20. if (value != path)
  21. {
  22. if (directoryPrefabs == null)
  23. {
  24. directoryPrefabs = new List<string>();
  25. }
  26. else
  27. {
  28. directoryPrefabs.Clear();
  29. }
  30. if (AllAssetPaths == null)
  31. {
  32. AllAssetPaths = new List<string>();
  33. }
  34. else
  35. {
  36. AllAssetPaths.Clear();
  37. }
  38. path = value;
  39. }
  40. }
  41. }
  42. static EditorWindow myWindow;
  43. static List<string> directoryPrefabs;
  44. static List<string> AllAssetPaths;
  45. bool showPrefabs = true;
  46. private static TMP_FontAsset tmp_Font = null;
  47. readonly string[] ReplaceMode = { "批量替换", "单个替换" };
  48. int replaceModeIndex;
  49. int ReplaceModeIndex
  50. {
  51. get
  52. {
  53. return replaceModeIndex;
  54. }
  55. set
  56. {
  57. if (value != replaceModeIndex)
  58. {
  59. if (directoryPrefabs == null)
  60. {
  61. directoryPrefabs = new List<string>();
  62. }
  63. else
  64. {
  65. directoryPrefabs.Clear();
  66. }
  67. if (AllAssetPaths == null)
  68. {
  69. AllAssetPaths = new List<string>();
  70. }
  71. else
  72. {
  73. AllAssetPaths.Clear();
  74. }
  75. Path = "";
  76. replaceModeIndex = value;
  77. }
  78. }
  79. }
  80. bool isAutoFixLinkedScripts = true;
  81. [MenuItem("切换/面板",false,1)]
  82. static void Init()
  83. {
  84. myWindow = EditorWindow.GetWindow(typeof(Text2TextMeshProUtil));
  85. directoryPrefabs = new List<string>();
  86. AllAssetPaths = new List<string>();
  87. scriptsFolders = new List<string> { "Assets" };
  88. myWindow.minSize = new Vector2(500, 300);
  89. }
  90. void OnGUI()
  91. {
  92. //isAutoFixLinkedScripts = EditorGUILayout.BeginToggleGroup("修改关联脚本", isAutoFixLinkedScripts);
  93. //ShowScriptsFolders();
  94. //EditorGUILayout.EndToggleGroup();
  95. //ReplaceModeIndex = GUILayout.Toolbar(ReplaceModeIndex, ReplaceMode);
  96. if (ReplaceModeIndex == 0)
  97. {
  98. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, new GUILayoutOption[] { GUILayout.Width(myWindow.position.width), GUILayout.Height(myWindow.position.height - scriptsFolders.Count * 20 - 70) });
  99. GetPath(true);
  100. ShowAllPrefabs();
  101. EditorGUILayout.EndScrollView();
  102. EditorGUILayout.BeginHorizontal();
  103. LoadPrefab();
  104. Text2TextMeshPro();
  105. EditorGUILayout.EndHorizontal();
  106. }
  107. else if (ReplaceModeIndex == 1)
  108. {
  109. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, new GUILayoutOption[] { GUILayout.Width(myWindow.position.width), GUILayout.Height(myWindow.position.height - scriptsFolders.Count * 20 - 70) });
  110. GetPath(false);
  111. ShowAllPrefabs();
  112. EditorGUILayout.EndScrollView();
  113. Text2TextMeshPro();
  114. }
  115. }
  116. void GetPath(bool isFolder = true)
  117. {
  118. Event e = Event.current;
  119. EditorGUILayout.BeginHorizontal();
  120. GUILayout.Label("Path:", GUILayout.Width(40));
  121. Path = GUILayout.TextField(Path);
  122. EditorGUILayout.EndHorizontal();
  123. if (Event.current.type == EventType.DragExited || Event.current.type == EventType.DragUpdated)
  124. {
  125. if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
  126. {
  127. DragAndDrop.visualMode = DragAndDropVisualMode.Link;
  128. if (Event.current.type == EventType.DragExited)
  129. {
  130. DragAndDrop.AcceptDrag();
  131. if (!isFolder)
  132. {
  133. if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length > 0)
  134. {
  135. Object[] objectReferences = DragAndDrop.objectReferences;
  136. for (int i = 0; i < objectReferences.Length; i++)
  137. {
  138. int index = i;
  139. if (AssetDatabase.GetAssetPath(objectReferences[index]).EndsWith(".prefab"))
  140. {
  141. if (!directoryPrefabs.Contains(AssetDatabase.GetAssetPath(objectReferences[index])))
  142. {
  143. directoryPrefabs.Add(AssetDatabase.GetAssetPath(objectReferences[index]));
  144. }
  145. }
  146. }
  147. }
  148. }
  149. else
  150. {
  151. if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
  152. {
  153. if (File.Exists(DragAndDrop.paths[0]))
  154. {
  155. EditorUtility.DisplayDialog("警告", "批量模式下请拖拽文件夹!", "确定");
  156. return;
  157. }
  158. Path = DragAndDrop.paths[0];
  159. }
  160. }
  161. }
  162. e.Use();
  163. }
  164. else
  165. {
  166. DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
  167. }
  168. }
  169. }
  170. void ShowAllPrefabs()
  171. {
  172. if (directoryPrefabs != null && directoryPrefabs.Count > 0)
  173. {
  174. showPrefabs = EditorGUILayout.Foldout(showPrefabs, "显示预制体");
  175. if (showPrefabs)
  176. {
  177. for (int i = 0; i < directoryPrefabs.Count; i++)
  178. {
  179. int index = i;
  180. EditorGUILayout.BeginHorizontal();
  181. EditorGUILayout.SelectableLabel($"预制体路径:{directoryPrefabs[index]}");
  182. if (GUILayout.Button("查看", GUILayout.Width(60)))
  183. {
  184. EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Object>(directoryPrefabs[index]));
  185. Selection.activeGameObject = AssetDatabase.LoadAssetAtPath<Object>(directoryPrefabs[index]) as GameObject;
  186. }
  187. if (GUILayout.Button("删除", GUILayout.Width(60)))
  188. {
  189. directoryPrefabs.RemoveAt(index);
  190. }
  191. EditorGUILayout.EndHorizontal();
  192. }
  193. }
  194. else
  195. {
  196. if (GUILayout.Button("清空选择", GUILayout.Width(60)))
  197. {
  198. directoryPrefabs.Clear();
  199. AllAssetPaths.Clear();
  200. Path = "";
  201. }
  202. }
  203. }
  204. }
  205. void LoadPrefab()
  206. {
  207. if (GUILayout.Button("加载预制体"))
  208. {
  209. if (!string.IsNullOrEmpty(Path))
  210. {
  211. DirectoryInfo direction = new DirectoryInfo(Path);
  212. FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
  213. for (int i = 0; i < files.Length; i++)
  214. {
  215. int startindex = files[i].FullName.IndexOf("Assets");
  216. string unityPath = files[i].FullName.Substring(startindex);
  217. if (!directoryPrefabs.Contains(unityPath))
  218. {
  219. directoryPrefabs.Add(unityPath);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. void Text2TextMeshPro()
  226. {
  227. if (directoryPrefabs != null && directoryPrefabs.Count > 0)
  228. {
  229. if (GUILayout.Button("一键替换"))
  230. {
  231. for (int i = 0; i < directoryPrefabs.Count; i++)
  232. {
  233. int index = i;
  234. Text2TextMeshPro(directoryPrefabs[index]);
  235. EditorUtility.DisplayProgressBar("替换进度", "当前进度", index / (float)directoryPrefabs.Count);
  236. }
  237. EditorUtility.ClearProgressBar();
  238. }
  239. }
  240. }
  241. void Text2TextMeshPro(string path)
  242. {
  243. GameObject root = PrefabUtility.LoadPrefabContents(path);
  244. if (root)
  245. {
  246. Text[] list = root.GetComponentsInChildren<Text>(true);
  247. for (int i = 0; i < list.Length; i++)
  248. {
  249. Text text = list[i];
  250. Transform target = text.transform;
  251. Vector2 size = text.rectTransform.sizeDelta;
  252. string strContent = text.text;
  253. Color color = text.color;
  254. int fontSize = text.fontSize;
  255. FontStyle fontStyle = text.fontStyle;
  256. TextAnchor textAnchor = text.alignment;
  257. bool richText = text.supportRichText;
  258. HorizontalWrapMode horizontalWrapMode = text.horizontalOverflow;
  259. VerticalWrapMode verticalWrapMode = text.verticalOverflow;
  260. bool raycastTarget = text.raycastTarget;
  261. GameObject.DestroyImmediate(text);
  262. TextMeshProUGUI textMeshPro = target.gameObject.AddComponent<TextMeshProUGUI>();
  263. if (tmp_Font == null)
  264. {
  265. tmp_Font = Resources.Load<TMP_FontAsset>("MyTextMesh");
  266. }
  267. textMeshPro.rectTransform.sizeDelta = size;
  268. textMeshPro.text = strContent;
  269. textMeshPro.color = color;
  270. textMeshPro.fontSize = fontSize;
  271. textMeshPro.fontStyle = fontStyle == FontStyle.BoldAndItalic ? FontStyles.Bold : (FontStyles)fontStyle;
  272. switch (textAnchor)
  273. {
  274. case TextAnchor.UpperLeft:
  275. textMeshPro.alignment = TextAlignmentOptions.TopLeft;
  276. break;
  277. case TextAnchor.UpperCenter:
  278. textMeshPro.alignment = TextAlignmentOptions.Top;
  279. break;
  280. case TextAnchor.UpperRight:
  281. textMeshPro.alignment = TextAlignmentOptions.TopRight;
  282. break;
  283. case TextAnchor.MiddleLeft:
  284. textMeshPro.alignment = TextAlignmentOptions.MidlineLeft;
  285. break;
  286. case TextAnchor.MiddleCenter:
  287. textMeshPro.alignment = TextAlignmentOptions.Midline;
  288. break;
  289. case TextAnchor.MiddleRight:
  290. textMeshPro.alignment = TextAlignmentOptions.MidlineRight;
  291. break;
  292. case TextAnchor.LowerLeft:
  293. textMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  294. break;
  295. case TextAnchor.LowerCenter:
  296. textMeshPro.alignment = TextAlignmentOptions.Bottom;
  297. break;
  298. case TextAnchor.LowerRight:
  299. textMeshPro.alignment = TextAlignmentOptions.BottomRight;
  300. break;
  301. }
  302. textMeshPro.richText = richText;
  303. if (verticalWrapMode == VerticalWrapMode.Overflow)
  304. {
  305. textMeshPro.enableWordWrapping = true;
  306. textMeshPro.overflowMode = TextOverflowModes.Overflow;
  307. }
  308. else
  309. {
  310. textMeshPro.enableWordWrapping = horizontalWrapMode == HorizontalWrapMode.Overflow ? false : true;
  311. }
  312. textMeshPro.raycastTarget = raycastTarget;
  313. }
  314. }
  315. PrefabUtility.SaveAsPrefabAsset(root, path, out bool success);
  316. if (!success)
  317. {
  318. Debug.LogError($"预制体:{path} 保存失败!");
  319. }
  320. if (isAutoFixLinkedScripts)
  321. {
  322. ChangeScriptsText2TextMeshPro();
  323. }
  324. }
  325. void ShowScriptsFolders()
  326. {
  327. for (int i = 0; i < scriptsFolders.Count; i++)
  328. {
  329. int index = i;
  330. EditorGUILayout.BeginHorizontal();
  331. SelectScriptsFolder(index);
  332. EditorGUILayout.EndHorizontal();
  333. }
  334. }
  335. void SelectScriptsFolder(int index)
  336. {
  337. Event e = Event.current;
  338. EditorGUILayout.BeginHorizontal();
  339. GUILayout.Label($"scriptsFolder{index + 1}:", GUILayout.Width(80));
  340. scriptsFolders[index] = GUILayout.TextField(scriptsFolders[index], GUILayout.Width(200));
  341. if (Event.current.type == EventType.DragExited || Event.current.type == EventType.DragUpdated)
  342. {
  343. if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
  344. {
  345. DragAndDrop.visualMode = DragAndDropVisualMode.Link;
  346. if (Event.current.type == EventType.DragExited)
  347. {
  348. DragAndDrop.AcceptDrag();
  349. if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
  350. {
  351. if (File.Exists(DragAndDrop.paths[0]))
  352. {
  353. EditorUtility.DisplayDialog("警告", "请选择文件夹!", "确定");
  354. return;
  355. }
  356. scriptsFolders[index] = DragAndDrop.paths[0];
  357. }
  358. }
  359. e.Use();
  360. }
  361. else
  362. {
  363. DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
  364. }
  365. }
  366. if (GUILayout.Button("添加路径"))
  367. {
  368. scriptsFolders.Add(scriptsFolders[scriptsFolders.Count - 1]);
  369. }
  370. if (GUILayout.Button("删除路径"))
  371. {
  372. if (scriptsFolders.Count == 1)
  373. {
  374. EditorUtility.DisplayDialog("警告", "仅剩最后一个文件夹,删除将会出错!", "确定");
  375. return;
  376. }
  377. scriptsFolders.RemoveAt(index);
  378. }
  379. EditorGUILayout.EndHorizontal();
  380. }
  381. void ChangeScriptsText2TextMeshPro()
  382. {
  383. AllAssetPaths.Clear();
  384. Debug.LogError(directoryPrefabs.Count);
  385. if (directoryPrefabs != null && directoryPrefabs.Count > 0)
  386. {
  387. for (int i = 0; i < directoryPrefabs.Count; i++)
  388. {
  389. int index = i;
  390. string scriptsName = System.IO.Path.GetFileNameWithoutExtension(directoryPrefabs[index]);
  391. string[] tmp = AssetDatabase.FindAssets($"{scriptsName} t:Script", scriptsFolders.ToArray());
  392. if (tmp != null && tmp.Length > 0)
  393. {
  394. AllAssetPaths.AddRange(tmp);
  395. }
  396. }
  397. for (int i = 0; i < AllAssetPaths.Count; i++)
  398. {
  399. int index = i;
  400. ChangeScriptsText2TextMeshPro(AssetDatabase.GUIDToAssetPath(AllAssetPaths[index]));
  401. }
  402. }
  403. AssetDatabase.Refresh();
  404. }
  405. void ChangeScriptsText2TextMeshPro(string script)
  406. {
  407. StreamReader sr = new StreamReader(script);
  408. string str = sr.ReadToEnd();
  409. sr.Close();
  410. str = str.Replace("<Text>", "<TMPro.TextMeshProUGUI>");
  411. str = str.Replace(" Text ", " TMPro.TextMeshProUGUI ");
  412. StreamWriter sw = new StreamWriter(script, false, System.Text.Encoding.UTF8);
  413. sw.Write(str);
  414. sw.Close();
  415. }
  416. }