源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

152 行
5.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// 替换预设中Text文字工具
  9. /// </summary>
  10. public class PrefabTextReplaceTools : EditorWindow
  11. {
  12. private static PrefabTextReplaceTools instance;
  13. static string _targetTxt; //被替换的文字内容
  14. static string _replaceTxt = ""; //要替换的文字内容
  15. static string _pathRoot; //查找文件路径
  16. static int _rate = 100;
  17. static int _ratePrefab = 10;
  18. static int _successCount = 0;
  19. private static void Init()
  20. {
  21. instance = EditorWindow.GetWindow<PrefabTextReplaceTools>("替换预设文字内容");
  22. _pathRoot = Application.dataPath + "/LuaFramework/AssetBundleRes/ui";
  23. instance.position = new Rect(new Vector2(604, 290), new Vector2(650, 400));
  24. }
  25. [MenuItem("Tools/替换预设Text组件text属性内容")]
  26. static void ShowExcelTools()
  27. {
  28. Init();
  29. instance.Show();
  30. }
  31. void OnGUI()
  32. {
  33. DrawOptions();
  34. }
  35. private void DrawOptions()
  36. {
  37. _targetTxt = EditorGUILayout.TextField("被替换文字内容", _targetTxt);
  38. _replaceTxt = EditorGUILayout.TextField("要替换文字内容", _replaceTxt);
  39. EditorGUILayout.Space();
  40. _pathRoot = EditorGUILayout.TextField("查找目录", _pathRoot);
  41. EditorGUILayout.Space();
  42. if (GUILayout.Button("转换"))
  43. {
  44. EditorCoroutineRunner.StartEditorCoroutine(Conver());
  45. }
  46. EditorGUILayout.Space();
  47. EditorGUILayout.Space();
  48. EditorGUILayout.Space();
  49. _rate = EditorGUILayout.IntField("每帧处理 文件 数", _rate);
  50. _ratePrefab = EditorGUILayout.IntField("每帧处理 预设 数", _ratePrefab);
  51. EditorGUILayout.HelpBox("每帧处理文件数,如果处理过程太卡,可以减少,当然也可以增加。\n目的是为了不让进程出现(未响应)", MessageType.Info);
  52. }
  53. IEnumerator Conver()
  54. {
  55. _successCount = 0;
  56. var files = Directory.GetFiles(_pathRoot, "*prefab*", SearchOption.AllDirectories);
  57. List<string> prefabsPath = new List<string>();
  58. //找到所有预设
  59. float count = 0;
  60. foreach (var file in files)
  61. {
  62. count++;
  63. if (file.EndsWith(".prefab"))
  64. {
  65. var clearPaht = file.Replace(Application.dataPath, "Assets");
  66. prefabsPath.Add(clearPaht);
  67. if (count % _rate == 0)
  68. {
  69. yield return 1;
  70. }
  71. EditorUtility.DisplayProgressBar("查找文件中的预设···", "查找中 " + file, count / files.Length);
  72. }
  73. }
  74. count = 0;
  75. foreach (var singlePaht in prefabsPath)
  76. {
  77. count++;
  78. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(singlePaht);
  79. if (prefab)
  80. {
  81. //boo = false;
  82. CheckSingPrefab(prefab);
  83. //if (boo)
  84. //{
  85. // Debug.Log(prefab.name);
  86. //}
  87. }
  88. if (count % _ratePrefab == 0)
  89. {
  90. yield return 1;
  91. }
  92. EditorUtility.DisplayProgressBar("替换当前预设中Text内容", "当前 " + singlePaht, count / prefabsPath.Count);
  93. }
  94. yield return 1;
  95. AssetDatabase.SaveAssets();
  96. AssetDatabase.Refresh();
  97. EditorUtility.ClearProgressBar();
  98. EditorUtility.DisplayDialog("提示", "查找完成,一共替换 " + _successCount + " 处", "好的呢", "");
  99. }
  100. //private bool boo = false;
  101. /// <summary>
  102. /// 循环遍历对象的每一个子对象,执行检查操作
  103. /// </summary>
  104. /// <param name="go"></param>
  105. void CheckSingPrefab(GameObject go)
  106. {
  107. CheckSingGameObject(go);
  108. foreach (Transform child in go.transform)
  109. {
  110. if (child.childCount > 0)
  111. {
  112. CheckSingPrefab(child.gameObject);
  113. }
  114. CheckSingGameObject(child.gameObject);
  115. }
  116. }
  117. /// <summary>
  118. /// 检查当前对象上是否有Text组件,如果有检查文字内容是否包含指定的内容,如果有就替换成要被替换的文字内容
  119. /// </summary>
  120. /// <param name="go"></param>
  121. void CheckSingGameObject(GameObject go)
  122. {
  123. var Txt = go.GetComponent<Text>();
  124. if (Txt)
  125. {
  126. string curStr = Txt.text;
  127. curStr = curStr.ToLower();
  128. if (curStr.Contains(_targetTxt))
  129. {
  130. string new_Str = curStr.Replace(_targetTxt, _replaceTxt);
  131. _successCount++;
  132. //boo = true;
  133. Txt.text = new_Str;
  134. EditorUtility.SetDirty(Txt); //必须要先一下这个 才能保存到磁盘 当资源已改变并需要保存到磁盘,Unity内部使用dirty标识来查找
  135. }
  136. }
  137. }
  138. }