AssetBundleCollector.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. namespace XGame.Editor.Build.AssetBundles
  5. {
  6. public class AssetBundleCollector
  7. {
  8. private BundleContext _context;
  9. private ShaderBundleCollector _shaderCollector;
  10. private AtlasBundleCollector _atlasCollector;
  11. private PackerBundleCollector _packerCollector;
  12. private DepBundleCollector _depCollector;
  13. private SceneBundleCollector _sceneCollector;
  14. public AssetBundleCollector(BundleContext context, ShaderBundleCollector shaderCollector, AtlasBundleCollector atlasCollector, PackerBundleCollector packerCollector, DepBundleCollector depCollector, SceneBundleCollector sceneCollector)
  15. {
  16. _context = context;
  17. _shaderCollector = shaderCollector;
  18. _atlasCollector = atlasCollector;
  19. _packerCollector = packerCollector;
  20. _depCollector = depCollector;
  21. _sceneCollector = sceneCollector;
  22. }
  23. /// <summary>
  24. /// 收集AssetBundle信息
  25. /// </summary>
  26. /// <param name="guidToAddressableName"></param>
  27. /// <returns></returns>
  28. public void CollectBundleBuilds(bool recursive)
  29. {
  30. //string root = PathDefine.ResAddressablePath;
  31. //Debug.Log($"CollectBundleBuilds Path: {root}");
  32. //List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
  33. //List<string> bundleNames = new List<string>();
  34. #region packer
  35. //var assetBundleBuilds = new Dictionary<long, AssetBundleBuild>();
  36. //var filePaths = new List<string>();
  37. // 先把BuildSetting中的Scene的path加到filePaths中,在之后的收集中过滤掉
  38. int sceneCount = EditorBuildSettings.scenes.Length;
  39. for (int i = 0; i < sceneCount; i++)
  40. _context.bundleAssetPaths.Add(EditorBuildSettings.scenes[i].path);
  41. _shaderCollector.Collect();
  42. if (_atlasCollector.Collect() && _packerCollector.Collect())
  43. {
  44. _atlasCollector.ToBundleBuilds();
  45. var waittingAssetMap = new Dictionary<string, string>();
  46. foreach (var waittingFile in _context.waittingAssetPaths)
  47. {
  48. if (_context.bundleAssetPaths.Contains(waittingFile))
  49. {
  50. continue;
  51. }
  52. var directoryPath = waittingFile.Substring(0, waittingFile.LastIndexOf('/'));
  53. waittingAssetMap.Add(waittingFile, directoryPath);
  54. }
  55. // 按文件夹名字分组
  56. var groups = waittingAssetMap.GroupBy((a) => a.Value);
  57. var assetPaths = new HashSet<string>();
  58. foreach(var group in groups)
  59. {
  60. var dirPath = group.Key;
  61. foreach (var item in group)
  62. {
  63. assetPaths.Add(item.Key);
  64. }
  65. _context.AddAddressableBundle(_context.ToArrayBySort(assetPaths), AssetDatabase.AssetPathToGUID(dirPath));
  66. assetPaths.Clear();
  67. }
  68. if (_context.waittingAssetPaths.Count > 0)
  69. {
  70. BuildLog.Error($"waittingAssetPaths 没有全部处理. 剩余数量:{_context.waittingAssetPaths.Count}");
  71. foreach (var waittingFile in _context.waittingAssetPaths)
  72. {
  73. BuildLog.Error($"waittingAssetPath:{waittingFile}");
  74. }
  75. }
  76. //var assetRoots = FileUtil.GetProductAssetsRoots();
  77. //foreach (var root in assetRoots)
  78. //{
  79. // FileUtil.FindFiles(root, (files) =>
  80. // {
  81. // if (_context.bundleAssetPaths != null && _context.bundleAssetPaths.Count > 0)
  82. // {
  83. // //过滤已设置bundle的资源,此处待优化
  84. // files = files.Where((file) => !_context.bundleAssetPaths.Contains(file)).ToArray();
  85. // }
  86. // if (files.Length > 0)
  87. // _context.bundleBuildMap.TryAdd(_context.CreateBundleBuild(files));
  88. // });
  89. //}
  90. if (recursive)
  91. _depCollector.Collect();
  92. }
  93. #endregion
  94. }
  95. #region Scene Bundle
  96. public bool CollectSceneBundles()
  97. {
  98. return _sceneCollector.Collect();
  99. }
  100. //public Dictionary<long, AssetBundleBuild> SceneBundleBuildMap => _sceneCollector.bundleBuildMap;
  101. //public bool MergeSceneBundles()
  102. //{
  103. // return _context.bundleBuildMap.TryAddRange(_sceneCollector.bundleBuildMap);
  104. //}
  105. #endregion
  106. }
  107. }