源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
14 KiB

  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEngine.UI;
  7. using System;
  8. public class ShowAllNotUseResourceCheck : EditorWindow
  9. {
  10. enum UIType
  11. {
  12. NotUseRes = 1,
  13. FindReferences = 2,
  14. }
  15. [MenuItem("Tools/删除资源工具", false, 1000)]
  16. static public void OpenPrefabTool()
  17. {
  18. uitype = UIType.NotUseRes;
  19. ShowAllNotUseResourceCheck checkWin = (ShowAllNotUseResourceCheck)EditorWindow.GetWindow<ShowAllNotUseResourceCheck>(false, "checkWin", true);
  20. checkWin.autoRepaintOnSceneChange = true;
  21. LoadConfig();
  22. checkWin.Show();
  23. }
  24. [MenuItem("Assets/Find References", false)]
  25. static public void FindResReferences()
  26. {
  27. uitype = UIType.FindReferences;
  28. StarFind();
  29. }
  30. static UIType uitype = UIType.NotUseRes;
  31. static Dictionary<string, bool> mapReourceUsedOther = new Dictionary<string, bool>(); //资源是否被代码引用
  32. static private List<string> checkType = new List<string>(); //检测的类型
  33. static private List<string> allResource = new List<string>(); //所有使用资源
  34. static private List<string> notUseReource = new List<string>(); //没有使用过的资源
  35. static private string searchModule = "bag"; //需要搜索的模块
  36. Vector2 scrollPos = Vector2.zero;
  37. static private string curSearchPath = ""; //当前搜索的图片的路径
  38. static private List<string> findPrefabs = new List<string>(); //被引用的预制体
  39. private void StartCheck()
  40. {
  41. List<string> withoutExtensions = new List<string>() { ".prefab" };
  42. string[] files = Directory.GetFiles(Application.dataPath + "/LuaFramework/AssetBundleRes/ui/", "*.*", SearchOption.AllDirectories)
  43. .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
  44. List<string> withoutExtensions2 = new List<string>() { ".png", ".jpg" };
  45. //所有project 里面的资源
  46. string[] UIReource = Directory.GetFiles(Application.dataPath + "/LuaFramework/AssetBundleRes/ui/" + searchModule, "*.*", SearchOption.AllDirectories)
  47. .Where(s => withoutExtensions2.Contains(Path.GetExtension(s).ToLower())).ToArray();
  48. for (int i = 0; i < UIReource.Length; i++)
  49. {
  50. UIReource[i] = UIReource[i].Replace("\\", "/");
  51. int index = UIReource[i].IndexOf("Assets");
  52. UIReource[i] = UIReource[i].Substring(index);
  53. }
  54. float count = 0;
  55. foreach (string file in files)
  56. {
  57. count++;
  58. EditorUtility.DisplayProgressBar("Processing...", "检索中....", count / files.Length);
  59. string strFile = file.Substring(file.IndexOf("Asset")).Replace('\\', '/');
  60. string[] dependenciesFiles = AssetDatabase.GetDependencies(strFile, false);
  61. foreach (string depFile in dependenciesFiles)
  62. {
  63. bool isNeedShow = false;
  64. foreach (string type in checkType)
  65. {
  66. //存在设置类型 需要显示
  67. if (depFile.IndexOf(type) > -1)
  68. {
  69. isNeedShow = true;
  70. break;
  71. }
  72. }
  73. if (isNeedShow == false)
  74. {
  75. continue;
  76. }
  77. allResource.Add(depFile);
  78. }
  79. }
  80. EditorUtility.ClearProgressBar();
  81. for (int i = 0; i < UIReource.Length; i++)
  82. {
  83. bool isUse = false;
  84. foreach (string usePath in allResource)
  85. {
  86. if (UIReource[i] == usePath)
  87. {
  88. isUse = true;
  89. break;
  90. }
  91. }
  92. if (isUse == false)
  93. {
  94. notUseReource.Add(UIReource[i]);
  95. }
  96. }
  97. }
  98. static void StarFind()
  99. {
  100. findPrefabs.Clear();
  101. if (Selection.activeObject == null)
  102. {
  103. EditorUtility.DisplayDialog("提示", "没有选中物体", "确定");
  104. return;
  105. }
  106. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  107. curSearchPath = path;
  108. if (string.IsNullOrEmpty(path))
  109. {
  110. EditorUtility.DisplayDialog("提示", "没有选中物体", "确定");
  111. return;
  112. }
  113. List<string> withoutExtensions = new List<string>() { ".prefab" };
  114. string[] files = Directory.GetFiles(Application.dataPath + "/LuaFramework/AssetBundleRes/ui/", "*.*", SearchOption.AllDirectories)
  115. .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
  116. float count = 0;
  117. foreach (string file in files)
  118. {
  119. count++;
  120. EditorUtility.DisplayProgressBar("Processing...", "检索中....", count / files.Length);
  121. string strFile = file.Substring(file.IndexOf("Asset")).Replace('\\', '/');
  122. string[] dependenciesFiles = AssetDatabase.GetDependencies(strFile, false);
  123. foreach (string depFile in dependenciesFiles)
  124. {
  125. if (path == depFile)
  126. {
  127. findPrefabs.Add(strFile);
  128. }
  129. }
  130. }
  131. EditorUtility.ClearProgressBar();
  132. if (findPrefabs.Count == 0)
  133. {
  134. EditorUtility.DisplayDialog("搜索结果", "没有找到任何预制体引用", "确定");
  135. }
  136. else
  137. {
  138. EditorUtility.DisplayDialog("搜索结果", "有" + findPrefabs.Count + "个预制体引用了该素材", "确定");
  139. ShowAllNotUseResourceCheck checkWin = (ShowAllNotUseResourceCheck)EditorWindow.GetWindow<ShowAllNotUseResourceCheck>(false, "checkWin", true);
  140. checkWin.autoRepaintOnSceneChange = true;
  141. checkWin.Show();
  142. }
  143. }
  144. void OnGUI()
  145. {
  146. if (uitype == UIType.NotUseRes)
  147. {
  148. OnNotUseResGUI();
  149. }
  150. else
  151. {
  152. OnFindReferencesGUI();
  153. }
  154. }
  155. void OnNotUseResGUI()
  156. {
  157. EditorGUILayout.BeginHorizontal();
  158. if (GUILayout.Button("列出所有未使用过的图片", GUILayout.Width(200)))
  159. {
  160. allResource = new List<string>();
  161. notUseReource = new List<string>();
  162. checkType.Clear();
  163. checkType.Add(".jpg");
  164. checkType.Add(".png");
  165. StartCheck();
  166. }
  167. EditorGUILayout.LabelField("搜索模块:", GUILayout.MaxWidth(45));
  168. searchModule = EditorGUILayout.TextField(searchModule, GUILayout.MaxWidth(200));
  169. EditorGUILayout.EndHorizontal();
  170. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  171. foreach (var path in notUseReource)
  172. {
  173. EditorGUILayout.BeginHorizontal();
  174. Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
  175. EditorGUILayout.ObjectField(t, typeof(Texture2D), false);
  176. string module = "", name = "";
  177. GetModuleAndName(path, out module, out name);
  178. EditorGUILayout.TextField(module, GUILayout.MaxWidth(100));
  179. EditorGUILayout.TextField(name, GUILayout.MaxWidth(200));
  180. GUI.backgroundColor = Color.red;
  181. if (GUILayout.Button("删除图片", GUILayout.Width(100)))
  182. {
  183. if (mapReourceUsedOther.ContainsKey(path))
  184. {
  185. mapReourceUsedOther.Remove(path);
  186. SaveConfig();
  187. }
  188. File.Delete(path);
  189. AssetDatabase.Refresh();
  190. }
  191. GUI.backgroundColor = Color.white;
  192. if (mapReourceUsedOther.ContainsKey(path) && mapReourceUsedOther[path] == true)
  193. {
  194. if (GUILayout.Button("取消", GUILayout.Width(200)))
  195. {
  196. mapReourceUsedOther[path] = false;
  197. SaveConfig();
  198. AssetDatabase.Refresh();
  199. }
  200. }
  201. else
  202. {
  203. if (GUILayout.Button("设置为代码使用", GUILayout.Width(200)))
  204. {
  205. if (mapReourceUsedOther.ContainsKey(path))
  206. mapReourceUsedOther[path] = true;
  207. else
  208. mapReourceUsedOther.Add(path, true);
  209. SaveConfig();
  210. AssetDatabase.Refresh();
  211. }
  212. }
  213. EditorGUILayout.EndHorizontal();
  214. }
  215. EditorGUILayout.EndScrollView();
  216. }
  217. void OnFindReferencesGUI()
  218. {
  219. EditorGUILayout.BeginHorizontal();
  220. GUI.backgroundColor = Color.green;
  221. if (GUILayout.Button("给所有预制件添加点击动画", GUILayout.Width(150)))
  222. {
  223. int i = 0;
  224. foreach (var path in findPrefabs)
  225. {
  226. AddButtonScale(path, true);
  227. i++;
  228. }
  229. AssetDatabase.Refresh();
  230. AssetDatabase.SaveAssets();
  231. EditorUtility.DisplayDialog("添加完成","添加了 " + i + "个预制件的点击动画", "确定");
  232. }
  233. EditorGUILayout.EndHorizontal();
  234. GUI.backgroundColor = Color.white;
  235. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  236. foreach (var path in findPrefabs)
  237. {
  238. EditorGUILayout.BeginHorizontal();
  239. GameObject t = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
  240. EditorGUILayout.ObjectField(t, typeof(GameObject), false);
  241. GUI.backgroundColor = Color.green;
  242. if (GUILayout.Button("打开预制体", GUILayout.Width(100)))
  243. {
  244. string[] files = Directory.GetFiles(Application.dataPath + "/LuaFramework/AssetBundleRes/ui/", t.name + ".prefab", SearchOption.AllDirectories);
  245. if (files.Length == 1)
  246. {
  247. U3DExtends.UIEditorHelper.LoadLayoutByPath(files[0]);
  248. }
  249. }
  250. GUI.backgroundColor = Color.green;
  251. if (GUILayout.Button("添加点击缩放动画", GUILayout.Width(150)))
  252. {
  253. AddButtonScale(path, false);
  254. }
  255. GUI.backgroundColor = Color.white;
  256. EditorGUILayout.EndHorizontal();
  257. }
  258. EditorGUILayout.EndScrollView();
  259. }
  260. void GetModuleAndName(string str, out string module, out string name)
  261. {
  262. string temp = "";
  263. module = "";
  264. name = "";
  265. int index = str.IndexOf("ui");
  266. temp = str.Substring(index);
  267. temp = temp.Replace(".png", "");
  268. string[] strs = temp.Split('/');
  269. if (strs.Length >= 4)
  270. {
  271. module = strs[1];
  272. name = strs[3];
  273. }
  274. }
  275. static void SaveConfig()
  276. {
  277. string path = Application.dataPath + @"/Editor/NotUseResourceCheck/NotUseResConfig.txt";
  278. StreamWriter sw;
  279. FileInfo fi = new FileInfo(path);
  280. sw = fi.CreateText();
  281. foreach (var item in mapReourceUsedOther)
  282. {
  283. if (item.Value)
  284. sw.WriteLine(item.Key);
  285. }
  286. sw.Close();
  287. sw.Dispose();
  288. }
  289. static void LoadConfig()
  290. {
  291. mapReourceUsedOther.Clear();
  292. string path = Application.dataPath + @"/Editor/NotUseResourceCheck/NotUseResConfig.txt";
  293. StreamReader sr = null;
  294. sr = File.OpenText(path);
  295. if (sr != null)
  296. {
  297. string t_Line;
  298. while ((t_Line = sr.ReadLine()) != null)
  299. {
  300. mapReourceUsedOther.Add(t_Line, true);
  301. }
  302. sr.Close();
  303. sr.Dispose();
  304. }
  305. }
  306. void AddButtonScale(string path, bool is_all)
  307. {
  308. string filePath = GetRelativeAssetsPath(path);
  309. Debug.Log("当前处理预制体 " + filePath);
  310. GameObject _prefab = AssetDatabase.LoadAssetAtPath(filePath, typeof(GameObject)) as GameObject;
  311. if (_prefab == null)
  312. {
  313. Debug.Log("该路径不能正常加载预制体 = >>> " + filePath);
  314. return;
  315. }
  316. GameObject find_obj = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;
  317. if (find_obj == null)
  318. {
  319. Debug.Log("该路径不实例化预制体 = >>> " + filePath);
  320. return;
  321. }
  322. bool isChange = false;
  323. string select_res_path = curSearchPath;
  324. Image[] imgs = find_obj.GetComponentsInChildren<Image>(true);
  325. foreach (var img in imgs)
  326. {
  327. Texture cur_texture = img.mainTexture;
  328. string cur_path = AssetDatabase.GetAssetPath(cur_texture);
  329. if (cur_path == select_res_path)
  330. {
  331. isChange = true;
  332. Debug.Log(cur_path + " 节点添加了点击动画");
  333. var com = img.gameObject.GetComponent<ExtendButtonScale>();
  334. if (!com)
  335. img.gameObject.AddComponent<ExtendButtonScale>();
  336. }
  337. }
  338. if (isChange)
  339. {
  340. PrefabUtility.ReplacePrefab(find_obj, _prefab, ReplacePrefabOptions.Default);
  341. }
  342. MonoBehaviour.DestroyImmediate(find_obj);
  343. if (isChange && !is_all)
  344. {
  345. AssetDatabase.Refresh();
  346. AssetDatabase.SaveAssets();
  347. }
  348. }
  349. private string GetRelativeAssetsPath(string path)
  350. {
  351. return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
  352. }
  353. }