AssetsDependenciesWindow.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace XGame.Editor.Asset.Tools
  8. {
  9. public class AssetsDependenciesWindow : EditorWindow
  10. {
  11. //[MenuItem("Assets/查找资源依赖")]
  12. public static void CreateWindow()
  13. {
  14. var paths = new List<string>();
  15. foreach (var obj in Selection.objects)
  16. {
  17. var assetPath = AssetDatabase.GetAssetPath(obj);
  18. if (Directory.Exists(assetPath))
  19. {
  20. var files = Directory.GetFiles(assetPath, "*", SearchOption.AllDirectories);
  21. foreach (var file in files)
  22. {
  23. paths.Add(FileUtil.ToRelativePath(file).Replace("\\", "/"));
  24. }
  25. }
  26. else
  27. {
  28. paths.Add(assetPath);
  29. }
  30. }
  31. paths = paths.Distinct().Where(p => FileUtil.IsFileIgnore(p) == false).ToList();
  32. if (paths.Count < 1) return;
  33. paths.Sort();
  34. var window = EditorWindow.GetWindow<AssetsDependenciesWindow>(true, "资源依赖窗口");
  35. window.Init(paths);
  36. window.Show();
  37. }
  38. private List<AssetDependenciesEditor> _lstDepEditor;
  39. private void Init(List<string> assetPaths)
  40. {
  41. _lstDepEditor = new List<AssetDependenciesEditor>();
  42. bool isFirst = true;
  43. var atlasRootPaths = FileUtil.GetAtlasRoots();
  44. foreach (var path in assetPaths)
  45. {
  46. var depEditor = new AssetDependenciesEditor(path, atlasRootPaths)
  47. {
  48. IsFoldout = isFirst
  49. };
  50. if (isFirst)
  51. {
  52. depEditor.Init();
  53. }
  54. isFirst = false;
  55. _lstDepEditor.Add(depEditor);
  56. }
  57. }
  58. //private string[] GetAtlasRootPaths()
  59. //{
  60. // var atlasRoots = Processor.AssetsConfig.AtlasRoots;
  61. // var rootPaths = Array.ConvertAll(atlasRoots, (root) => root.path);
  62. // return rootPaths;
  63. //}
  64. private Vector2 _scrollPosition;
  65. private void OnGUI()
  66. {
  67. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, false, true);
  68. EditorGUILayout.HelpBox("注意:\n1.图集资源需要先通过菜单: Build/CreateAtlas 创建图集后才能收集。\n2.资源显示红色表示该依赖资源在Res/Addressable目录下,需要检测该依赖资源是否需要移动到Res/Static目录。", MessageType.Warning);
  69. foreach (var depEditor in _lstDepEditor)
  70. {
  71. depEditor.OnGUI();
  72. }
  73. EditorGUILayout.EndScrollView();
  74. }
  75. private void OnDisable()
  76. {
  77. foreach (var depEditor in _lstDepEditor)
  78. {
  79. depEditor.Dispose();
  80. }
  81. _lstDepEditor.Clear();
  82. _lstDepEditor = null;
  83. //释放编辑器未使用的资源
  84. EditorUtility.UnloadUnusedAssetsImmediate();
  85. }
  86. }
  87. }