BundleContextBuilder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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)
  81. {
  82. return;
  83. }
  84. _context.bundleNameMode = packerManifest.bundleNameMode;
  85. _context.isMergeShader = packerManifest.isMergeShader;
  86. if (packerManifest.GroupInfos == null || packerManifest.GroupInfos.Count < 1)
  87. {
  88. return;
  89. }
  90. var groupNameMap = _context.groupNameMap;
  91. var directories = new HashSet<string>();
  92. foreach (var groupInfo in packerManifest.GroupInfos)
  93. {
  94. var groupName = groupInfo.groupName;
  95. if (string.IsNullOrEmpty(groupName) || Asset.FileUtil.HasChinese(groupName))
  96. {
  97. Debug.LogWarning($"[XBuild]AssetbundleGroup 名字为空或者包含中文字符. ID:{groupInfo.GroupId}");
  98. continue;
  99. }
  100. if (LanguageUtils.IsLanguage(groupName))
  101. {
  102. Debug.LogWarning($"[XBuild]AssetbundleGroup 名字不能为多语言枚举. GroupName:{groupName}");
  103. continue;
  104. }
  105. groupName = groupName.ToLower();
  106. foreach (var guid in groupInfo.guidList)
  107. {
  108. if (string.IsNullOrEmpty(guid))
  109. {
  110. continue;
  111. }
  112. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  113. if (!assetPath.StartsWith(PathDefine.ResAddressableRelative))
  114. {
  115. Debug.LogWarning($"[XBuild]AssetbundleGroup 路径错误. GroupName:{groupName} assetPath:{assetPath}");
  116. continue;
  117. }
  118. if (Directory.Exists(assetPath))
  119. {
  120. directories.Add(assetPath);
  121. ////是文件夹
  122. //var filePaths = FileUtil.FindFiles(assetPath);
  123. //foreach (var filePath in filePaths)
  124. //{
  125. // AddGroupItem(filePath, groupName, groupNameMap);
  126. //}
  127. continue;
  128. }
  129. AddGroupItem(assetPath, groupName, groupNameMap);
  130. }
  131. if (directories.Count > 0)
  132. {
  133. AddGroupItem(directories.ToArray(), groupName, groupNameMap);
  134. directories.Clear();
  135. }
  136. }
  137. Debug.Log($"CollectBundleGroupNames Count:{groupNameMap.Count}");
  138. }
  139. private void AddGroupItem(string[] directories, string groupName, Dictionary<string, string> groupNameMap)
  140. {
  141. var guids = AssetDatabase.FindAssets("", directories);
  142. foreach (var guid in guids)
  143. {
  144. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  145. //if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath))
  146. //{
  147. // continue;
  148. //}
  149. AddGroupItem(assetPath, groupName, groupNameMap);
  150. }
  151. }
  152. private void AddGroupItem(string assetPath, string groupName, Dictionary<string, string> groupNameMap)
  153. {
  154. if (!_context.waittingAssetPaths.Contains(assetPath))
  155. {
  156. return;
  157. }
  158. //if (!_context.TryGetAddressableNameByPath(assetPath, out var addressableName))
  159. //{
  160. // return;
  161. //}
  162. if (groupNameMap.ContainsKey(assetPath))
  163. {
  164. // 先到先得
  165. return;
  166. }
  167. //Debug.Log($"[XBuild]AddGroupItem addressableName:{addressableName} groupName:{groupName}");
  168. groupNameMap.Add(assetPath, groupName);
  169. }
  170. }
  171. }