AtlasBundleCollector.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.U2D;
  6. using UnityEngine.U2D;
  7. namespace XGame.Editor.Build.AssetBundles
  8. {
  9. public class AtlasBundleCollector
  10. {
  11. private class AtlasInfo
  12. {
  13. public string key;
  14. public string[] assetPaths;
  15. }
  16. private BundleContext _context;
  17. /// <summary>
  18. /// key: sprite的assetPath
  19. /// value:AtlasInfos
  20. /// </summary>
  21. private Dictionary<string, AtlasInfo> _atlasInfoMap;
  22. public AtlasBundleCollector(BundleContext context)
  23. {
  24. _context = context;
  25. _atlasInfoMap = new Dictionary<string, AtlasInfo>();
  26. }
  27. public bool Collect()
  28. {
  29. var directories = Asset.FileUtil.GetAtlasRoots();
  30. var assetPaths = new HashSet<string>();
  31. var registedPaths = new HashSet<string>();
  32. //图集的alpha texture信息
  33. //var atlasAlphaMap = AtlasHelper.GetAtlasAlphaTextureMap();
  34. var guids = AssetDatabase.FindAssets("t:SpriteAtlas", directories);
  35. foreach (var guid in guids)
  36. {
  37. var atlasPath = AssetDatabase.GUIDToAssetPath(guid);
  38. var atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(atlasPath);
  39. //Debug.Log($"atlasPath:{atlasPath} name:{atlas.name}", atlas);
  40. var objs = atlas.GetPackables();
  41. foreach (var obj in objs)
  42. {
  43. var assetPath = AssetDatabase.GetAssetPath(obj);
  44. if (Directory.Exists(assetPath))
  45. {
  46. //是文件夹
  47. var temps = Asset.FileUtil.FindFiles(assetPath, Asset.FileUtil.texturePatterns, SearchOption.TopDirectoryOnly);
  48. foreach (var temp in temps)
  49. {
  50. if (!registedPaths.Contains(temp) && !assetPaths.Contains(temp))
  51. {
  52. registedPaths.Add(temp);
  53. assetPaths.Add(temp);
  54. }
  55. }
  56. }
  57. else if (!registedPaths.Contains(assetPath) && !assetPaths.Contains(assetPath))
  58. {
  59. // 贴图
  60. registedPaths.Add(assetPath);
  61. assetPaths.Add(assetPath);
  62. }
  63. }
  64. //if (!_context.filePaths.Contains(atlasPath) && !assetPaths.Contains(atlasPath))
  65. //{
  66. // // SpriteAtlas文件,sbp打包依赖该文件过滤贴图的原始文件,因此该文件也需要参与打包
  67. // assetPaths.Add(atlasPath);
  68. // _context.filePaths.Add(atlasPath);
  69. //}
  70. //alpha texture也打包进图集
  71. //if (atlasAlphaMap.TryGetValue(atlas.name, out var alphaTextureGUIDs))
  72. //{
  73. // foreach (var alphaGUID in alphaTextureGUIDs)
  74. // {
  75. // var alphaAssetPath = AssetDatabase.GUIDToAssetPath(alphaGUID);
  76. // Debug.Log($"Atals Alpha Texture Path:{alphaAssetPath}", AssetDatabase.LoadMainAssetAtPath(alphaAssetPath));
  77. // if (!_context.filePaths.Contains(alphaAssetPath) && !assetPaths.Contains(alphaAssetPath))
  78. // {
  79. // assetPaths.Add(alphaAssetPath);
  80. // _context.filePaths.Add(alphaAssetPath);
  81. // }
  82. // }
  83. //}
  84. if (assetPaths.Count > 0)
  85. {
  86. var assetNames = _context.ToArrayBySort(assetPaths);
  87. assetPaths.Clear();
  88. if (atlasPath.Contains(_context.ResStaticDir))
  89. {
  90. _context.AddRawAssetBundle(assetNames, guid, false);
  91. }
  92. else
  93. {
  94. // 允许Addressable的图集合并打在同一个ab里
  95. var info = new AtlasInfo()
  96. {
  97. key = guid,
  98. assetPaths = assetNames
  99. };
  100. foreach (var assetPath in assetNames)
  101. {
  102. _atlasInfoMap.Add(assetPath, info);
  103. }
  104. // TODO
  105. //_context.bundleBuildMap.TryAdd(_context.CreateBundleBuild(assetNames));
  106. }
  107. }
  108. }
  109. return true;
  110. }
  111. public bool IsAtlasAssetPath(string assetPath)
  112. {
  113. return _atlasInfoMap.ContainsKey(assetPath);
  114. }
  115. public string[] PopAtlasAssetPaths(string assetPath)
  116. {
  117. var info = _atlasInfoMap[assetPath];
  118. foreach (var path in info.assetPaths)
  119. {
  120. _atlasInfoMap.Remove(path);
  121. }
  122. return info.assetPaths;
  123. }
  124. /// <summary>
  125. /// 处理剩余的Addressable的图集
  126. /// </summary>
  127. public void ToBundleBuilds()
  128. {
  129. var values = _atlasInfoMap.Values.Distinct();
  130. foreach (var info in values)
  131. {
  132. _context.AddAddressableBundle(info.assetPaths, info.key);
  133. }
  134. _atlasInfoMap.Clear();
  135. }
  136. }
  137. }