PackerBundleCollector.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace XGame.Editor.Build.AssetBundles
  5. {
  6. public class PackerBundleCollector
  7. {
  8. private BundleContext _context;
  9. private AtlasBundleCollector _atlasCollector;
  10. public PackerBundleCollector(BundleContext context, AtlasBundleCollector atlasCollector)
  11. {
  12. _context = context;
  13. _atlasCollector = atlasCollector;
  14. }
  15. /// <summary>
  16. /// 根据设置的分包规则收集AssetBundle信息
  17. /// </summary>
  18. /// <param name="guidToAddressableName"></param>
  19. /// <param name="assetBundleBuilds">bundle信息集合</param>
  20. /// <param name="filePaths">收集到的bundle所包含的资源路径集合,只收集ResAddressable目录</param>
  21. /// <returns></returns>
  22. public bool Collect()
  23. {
  24. //var tempFilePaths = new List<string>();
  25. var packerManifest = ABPackerInfoManifest.Load();
  26. if (packerManifest == null)
  27. {
  28. return true;
  29. }
  30. var packerInfos = packerManifest.PackerInfos;
  31. packerInfos.Sort();
  32. for (int i = 0; i < packerInfos.Count; i++)
  33. {
  34. var info = packerInfos[i];
  35. var assetPath = info.AssetPath;
  36. if (!_context.IsValidPath(assetPath))
  37. {
  38. BuildLog.Warn($"ABPackerInfo invalid.{info}");
  39. continue;
  40. }
  41. //Debug.Log($"Packer index:{i} {info} full: {System.IO.Path.GetFullPath(assetPath)}");
  42. if (info.packerType == ABPackerType.SingleAsset)
  43. {
  44. //var langFlag = AddressableHelper.GetLanguageFlagByAssetPath(assetPath);
  45. if (info.IsFile)
  46. {
  47. AddSingleBundle(assetPath, info.GUID);
  48. }
  49. else
  50. {
  51. var guids = AssetDatabase.FindAssets("", new string[] { assetPath });
  52. foreach (var guid in guids)
  53. {
  54. AddSingleBundle(AssetDatabase.GUIDToAssetPath(guid), guid);
  55. }
  56. }
  57. }
  58. else if (info.packerType == ABPackerType.AllDirectories)
  59. {
  60. AddAllDirectoriesBundle(assetPath, info.GUID);
  61. }
  62. }
  63. //filePaths.AddRange(tempFilePaths);
  64. return true;
  65. }
  66. private void AddSingleBundle(string assetPath, string guid)
  67. {
  68. if (_context.waittingAssetPaths.Contains(assetPath) && _context.bundleAssetPaths.Contains(assetPath) == false && _atlasCollector.IsAtlasAssetPath(assetPath) == false)
  69. { // 配置了单体,但是是图集的也忽略
  70. _context.AddAddressableBundle(new string[] { assetPath }, guid);
  71. //var bundleName = $"single_{System.IO.Path.GetFileNameWithoutExtension(assetPath)}_{info.GUID.Substring(0, 8)}".ToLower();
  72. //if (langFlag != Framework.i18n.LanguageType.NONE)
  73. //{
  74. // bundleName = $"{langFlag.ToString().ToLower()}/{bundleName}";
  75. //}
  76. //_context.bundleBuildMap.TryAdd(_context.CreateBundleBuild(new string[] { assetPath }, bundleName.ToLower()));
  77. }
  78. }
  79. private HashSet<string> _tempAssetPaths = new HashSet<string>();
  80. private void AddAllDirectoriesBundle(string directoryPath, string directoryGuid)
  81. {
  82. var guids = AssetDatabase.FindAssets("", new string[] { directoryPath });
  83. foreach (var fileGuid in guids)
  84. {
  85. var filePath = AssetDatabase.GUIDToAssetPath(fileGuid);
  86. if (_context.waittingAssetPaths.Contains(filePath) && _context.bundleAssetPaths.Contains(filePath) == false && _tempAssetPaths.Contains(filePath) == false)
  87. {
  88. if (_atlasCollector.IsAtlasAssetPath(filePath))
  89. {
  90. _tempAssetPaths.UnionWith(_atlasCollector.PopAtlasAssetPaths(filePath));
  91. }
  92. else
  93. {
  94. _tempAssetPaths.Add(filePath);
  95. }
  96. }
  97. }
  98. if (_tempAssetPaths.Count > 0)
  99. {
  100. _context.AddAddressableBundle(_context.ToArrayBySort(_tempAssetPaths), directoryGuid);
  101. _tempAssetPaths.Clear();
  102. }
  103. else
  104. {
  105. BuildLog.Warn($"Packer config invalid.Path:{directoryPath} GUID:{directoryGuid}");
  106. }
  107. }
  108. }
  109. }