BundleContextBuilder.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using XGame.Editor.Asset;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using XGame.Framework.i18n;
  8. namespace XGame.Editor.Build.AssetBundles
  9. {
  10. public class BundleContextBuilder
  11. {
  12. private BundleContext _context;
  13. public BundleContext Product => _context;
  14. public bool Build()
  15. {
  16. if (!CreatContext())
  17. {
  18. return false;
  19. }
  20. CollectProductAssets();
  21. CollectTextures();
  22. CollectBundleGroupNames();
  23. return true;
  24. }
  25. private bool CreatContext()
  26. {
  27. //可寻址资源清单
  28. var assetInfoManifest = AddressableHelper.LoadAssetManifest();
  29. if (assetInfoManifest == null)
  30. {
  31. Debug.LogError("Can't find AddressableAssetInfoManifest.");
  32. return false;
  33. }
  34. var assetInfoMap = assetInfoManifest.GetAssetInfoMapWithGUID();
  35. _context = new BundleContext(assetInfoMap);
  36. return true;
  37. }
  38. private void CollectProductAssets()
  39. {
  40. var productAssetsRoots = Asset.FileUtil.GetProductAssetsRoots();
  41. var assetGuids = AssetDatabase.FindAssets("", productAssetsRoots);
  42. foreach (var guid in assetGuids)
  43. {
  44. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  45. if (Directory.Exists(assetPath))
  46. {
  47. //_context.directoriesMap.Add(assetPath, guid);
  48. continue;
  49. }
  50. else if (Asset.FileUtil.IsFileIgnore(assetPath))
  51. {
  52. continue;
  53. }
  54. _context.waittingAssetPaths.Add(assetPath);
  55. }
  56. _context.assetsRoots = productAssetsRoots;
  57. }
  58. private void CollectTextures()
  59. {
  60. var guids = AssetDatabase.FindAssets("t:Texture", new string[] { PathDefine.ResAddressableRelative, PathDefine.I18nAssetsRelative, PathDefine.ResStaticRelative });
  61. foreach (var guid in guids)
  62. {
  63. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  64. if (string.IsNullOrEmpty(assetPath))
  65. {
  66. continue;
  67. }
  68. _context.allTexturePaths.Add(assetPath);
  69. }
  70. }
  71. /// <summary>
  72. /// 收集Assetbundle分组的GroupName
  73. /// <key: assetPath, value: groupName>
  74. /// </summary>
  75. /// <param name="guidToAddressableName"></param>
  76. /// <returns></returns>
  77. private void CollectBundleGroupNames()
  78. {
  79. var packerManifest = ABPackerInfoManifest.Load();
  80. if (packerManifest == null || packerManifest.GroupInfos == null || packerManifest.GroupInfos.Count < 1)
  81. {
  82. return;
  83. }
  84. _context.bundleNameMode = packerManifest.bundleNameMode;
  85. _context.isMergeShader = packerManifest.isMergeShader;
  86. var groupNameMap = _context.groupNameMap;
  87. var directories = new HashSet<string>();
  88. foreach (var groupInfo in packerManifest.GroupInfos)
  89. {
  90. var groupName = groupInfo.groupName;
  91. if (string.IsNullOrEmpty(groupName) || Asset.FileUtil.HasChinese(groupName))
  92. {
  93. Debug.LogWarning($"[XBuild]AssetbundleGroup 名字为空或者包含中文字符. ID:{groupInfo.GroupId}");
  94. continue;
  95. }
  96. if (LanguageUtils.IsLanguage(groupName))
  97. {
  98. Debug.LogWarning($"[XBuild]AssetbundleGroup 名字不能为多语言枚举. GroupName:{groupName}");
  99. continue;
  100. }
  101. groupName = groupName.ToLower();
  102. foreach (var guid in groupInfo.guidList)
  103. {
  104. if (string.IsNullOrEmpty(guid))
  105. {
  106. continue;
  107. }
  108. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  109. if (!assetPath.StartsWith(PathDefine.ResAddressableRelative))
  110. {
  111. Debug.LogWarning($"[XBuild]AssetbundleGroup 路径错误. GroupName:{groupName} assetPath:{assetPath}");
  112. continue;
  113. }
  114. if (Directory.Exists(assetPath))
  115. {
  116. directories.Add(assetPath);
  117. ////是文件夹
  118. //var filePaths = FileUtil.FindFiles(assetPath);
  119. //foreach (var filePath in filePaths)
  120. //{
  121. // AddGroupItem(filePath, groupName, groupNameMap);
  122. //}
  123. continue;
  124. }
  125. AddGroupItem(assetPath, groupName, groupNameMap);
  126. }
  127. if (directories.Count > 0)
  128. {
  129. AddGroupItem(directories.ToArray(), groupName, groupNameMap);
  130. directories.Clear();
  131. }
  132. }
  133. Debug.Log($"CollectBundleGroupNames Count:{groupNameMap.Count}");
  134. }
  135. private void AddGroupItem(string[] directories, string groupName, Dictionary<string, string> groupNameMap)
  136. {
  137. var guids = AssetDatabase.FindAssets("", directories);
  138. foreach (var guid in guids)
  139. {
  140. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  141. //if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath))
  142. //{
  143. // continue;
  144. //}
  145. AddGroupItem(assetPath, groupName, groupNameMap);
  146. }
  147. }
  148. private void AddGroupItem(string assetPath, string groupName, Dictionary<string, string> groupNameMap)
  149. {
  150. if (!_context.waittingAssetPaths.Contains(assetPath))
  151. {
  152. return;
  153. }
  154. //if (!_context.TryGetAddressableNameByPath(assetPath, out var addressableName))
  155. //{
  156. // return;
  157. //}
  158. if (groupNameMap.ContainsKey(assetPath))
  159. {
  160. // 先到先得
  161. return;
  162. }
  163. //Debug.Log($"[XBuild]AddGroupItem addressableName:{addressableName} groupName:{groupName}");
  164. groupNameMap.Add(assetPath, groupName);
  165. }
  166. }
  167. }