AssetBundleCollector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace YooAsset.Editor
  7. {
  8. [Serializable]
  9. public class AssetBundleCollector
  10. {
  11. /// <summary>
  12. /// 收集路径
  13. /// 注意:支持文件夹或单个资源文件
  14. /// </summary>
  15. public string CollectPath = string.Empty;
  16. /// <summary>
  17. /// 收集器的GUID
  18. /// </summary>
  19. public string CollectorGUID = string.Empty;
  20. /// <summary>
  21. /// 收集器类型
  22. /// </summary>
  23. public ECollectorType CollectorType = ECollectorType.MainAssetCollector;
  24. /// <summary>
  25. /// 寻址规则类名
  26. /// </summary>
  27. public string AddressRuleName = nameof(AddressByFileName);
  28. /// <summary>
  29. /// 打包规则类名
  30. /// </summary>
  31. public string PackRuleName = nameof(PackDirectory);
  32. /// <summary>
  33. /// 过滤规则类名
  34. /// </summary>
  35. public string FilterRuleName = nameof(CollectAll);
  36. /// <summary>
  37. /// 资源分类标签
  38. /// </summary>
  39. public string AssetTags = string.Empty;
  40. /// <summary>
  41. /// 用户自定义数据
  42. /// </summary>
  43. public string UserData = string.Empty;
  44. /// <summary>
  45. /// 收集器是否有效
  46. /// </summary>
  47. public bool IsValid()
  48. {
  49. if (AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(CollectPath) == null)
  50. return false;
  51. if (CollectorType == ECollectorType.None)
  52. return false;
  53. if (AssetBundleCollectorSettingData.HasAddressRuleName(AddressRuleName) == false)
  54. return false;
  55. if (AssetBundleCollectorSettingData.HasPackRuleName(PackRuleName) == false)
  56. return false;
  57. if (AssetBundleCollectorSettingData.HasFilterRuleName(FilterRuleName) == false)
  58. return false;
  59. return true;
  60. }
  61. /// <summary>
  62. /// 检测配置错误
  63. /// </summary>
  64. public void CheckConfigError()
  65. {
  66. string assetGUID = AssetDatabase.AssetPathToGUID(CollectPath);
  67. if (string.IsNullOrEmpty(assetGUID))
  68. throw new Exception($"Invalid collect path : {CollectPath}");
  69. if (CollectorType == ECollectorType.None)
  70. throw new Exception($"{nameof(ECollectorType)}.{ECollectorType.None} is invalid in collector : {CollectPath}");
  71. if (AssetBundleCollectorSettingData.HasPackRuleName(PackRuleName) == false)
  72. throw new Exception($"Invalid {nameof(IPackRule)} class type : {PackRuleName} in collector : {CollectPath}");
  73. if (AssetBundleCollectorSettingData.HasFilterRuleName(FilterRuleName) == false)
  74. throw new Exception($"Invalid {nameof(IFilterRule)} class type : {FilterRuleName} in collector : {CollectPath}");
  75. if (AssetBundleCollectorSettingData.HasAddressRuleName(AddressRuleName) == false)
  76. throw new Exception($"Invalid {nameof(IAddressRule)} class type : {AddressRuleName} in collector : {CollectPath}");
  77. }
  78. /// <summary>
  79. /// 修复配置错误
  80. /// </summary>
  81. public bool FixConfigError()
  82. {
  83. bool isFixed = false;
  84. if (string.IsNullOrEmpty(CollectorGUID) == false)
  85. {
  86. string convertAssetPath = AssetDatabase.GUIDToAssetPath(CollectorGUID);
  87. if (string.IsNullOrEmpty(convertAssetPath))
  88. {
  89. Debug.LogWarning($"Collector GUID {CollectorGUID} is invalid and has been auto removed !");
  90. CollectorGUID = string.Empty;
  91. isFixed = true;
  92. }
  93. else
  94. {
  95. if (CollectPath != convertAssetPath)
  96. {
  97. CollectPath = convertAssetPath;
  98. isFixed = true;
  99. Debug.LogWarning($"Fix collect path : {CollectPath} -> {convertAssetPath}");
  100. }
  101. }
  102. }
  103. /*
  104. string convertGUID = AssetDatabase.AssetPathToGUID(CollectPath);
  105. if(string.IsNullOrEmpty(convertGUID) == false)
  106. {
  107. CollectorGUID = convertGUID;
  108. }
  109. */
  110. return isFixed;
  111. }
  112. /// <summary>
  113. /// 获取打包收集的资源文件
  114. /// </summary>
  115. public List<CollectAssetInfo> GetAllCollectAssets(CollectCommand command, AssetBundleCollectorGroup group)
  116. {
  117. // 注意:模拟构建模式下只收集主资源
  118. if (command.BuildMode == EBuildMode.SimulateBuild)
  119. {
  120. if (CollectorType != ECollectorType.MainAssetCollector)
  121. return new List<CollectAssetInfo>();
  122. }
  123. Dictionary<string, CollectAssetInfo> result = new Dictionary<string, CollectAssetInfo>(1000);
  124. // 检测是否为原生资源打包规则
  125. IPackRule packRuleInstance = AssetBundleCollectorSettingData.GetPackRuleInstance(PackRuleName);
  126. bool isRawFilePackRule = packRuleInstance.IsRawFilePackRule();
  127. // 检测原生资源包的收集器类型
  128. if (isRawFilePackRule && CollectorType != ECollectorType.MainAssetCollector)
  129. throw new Exception($"The raw file pack rule must be set to {nameof(ECollectorType)}.{ECollectorType.MainAssetCollector} : {CollectPath}");
  130. if (string.IsNullOrEmpty(CollectPath))
  131. throw new Exception($"The collect path is null or empty in group : {group.GroupName}");
  132. // 收集打包资源
  133. if (AssetDatabase.IsValidFolder(CollectPath))
  134. {
  135. string collectDirectory = CollectPath;
  136. string[] findAssets = EditorTools.FindAssets(EAssetSearchType.All, collectDirectory);
  137. foreach (string assetPath in findAssets)
  138. {
  139. if (IsValidateAsset(assetPath, isRawFilePackRule) && IsCollectAsset(assetPath))
  140. {
  141. if (result.ContainsKey(assetPath) == false)
  142. {
  143. var collectAssetInfo = CreateCollectAssetInfo(command, group, assetPath, isRawFilePackRule);
  144. result.Add(assetPath, collectAssetInfo);
  145. }
  146. else
  147. {
  148. throw new Exception($"The collecting asset file is existed : {assetPath} in collector : {CollectPath}");
  149. }
  150. }
  151. }
  152. }
  153. else
  154. {
  155. string assetPath = CollectPath;
  156. if (IsValidateAsset(assetPath, isRawFilePackRule) && IsCollectAsset(assetPath))
  157. {
  158. var collectAssetInfo = CreateCollectAssetInfo(command, group, assetPath, isRawFilePackRule);
  159. result.Add(assetPath, collectAssetInfo);
  160. }
  161. else
  162. {
  163. throw new Exception($"The collecting single asset file is invalid : {assetPath} in collector : {CollectPath}");
  164. }
  165. }
  166. // 检测可寻址地址是否重复
  167. if (command.EnableAddressable)
  168. {
  169. var addressTemper = new Dictionary<string, string>();
  170. foreach (var collectInfoPair in result)
  171. {
  172. if (collectInfoPair.Value.CollectorType == ECollectorType.MainAssetCollector)
  173. {
  174. string address = collectInfoPair.Value.Address;
  175. string assetPath = collectInfoPair.Value.AssetPath;
  176. if (addressTemper.TryGetValue(address, out var existed) == false)
  177. addressTemper.Add(address, assetPath);
  178. else
  179. throw new Exception($"The address is existed : {address} in collector : {CollectPath} \nAssetPath:\n {existed}\n {assetPath}");
  180. }
  181. }
  182. }
  183. // 返回列表
  184. return result.Values.ToList();
  185. }
  186. private CollectAssetInfo CreateCollectAssetInfo(CollectCommand command, AssetBundleCollectorGroup group, string assetPath, bool isRawFilePackRule)
  187. {
  188. string address = GetAddress(command, group, assetPath);
  189. string bundleName = GetBundleName(command, group, assetPath);
  190. List<string> assetTags = GetAssetTags(group);
  191. CollectAssetInfo collectAssetInfo = new CollectAssetInfo(CollectorType, bundleName, address, assetPath, isRawFilePackRule, assetTags);
  192. // 注意:模拟构建模式下不需要收集依赖资源
  193. if (command.BuildMode == EBuildMode.SimulateBuild)
  194. collectAssetInfo.DependAssets = new List<string>();
  195. else
  196. collectAssetInfo.DependAssets = GetAllDependencies(assetPath);
  197. return collectAssetInfo;
  198. }
  199. private bool IsValidateAsset(string assetPath, bool isRawFilePackRule)
  200. {
  201. if (assetPath.StartsWith("Assets/") == false && assetPath.StartsWith("Packages/") == false)
  202. {
  203. UnityEngine.Debug.LogError($"Invalid asset path : {assetPath}");
  204. return false;
  205. }
  206. // 忽略文件夹
  207. if (AssetDatabase.IsValidFolder(assetPath))
  208. return false;
  209. // 忽略编辑器下的类型资源
  210. Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
  211. if (assetType == typeof(LightingDataAsset))
  212. return false;
  213. // 检测原生文件是否合规
  214. if (isRawFilePackRule)
  215. {
  216. string extension = EditorTools.RemoveFirstChar(System.IO.Path.GetExtension(assetPath));
  217. if (extension == EAssetFileExtension.unity.ToString() || extension == EAssetFileExtension.prefab.ToString() ||
  218. extension == EAssetFileExtension.fbx.ToString() || extension == EAssetFileExtension.mat.ToString() ||
  219. extension == EAssetFileExtension.controller.ToString() || extension == EAssetFileExtension.anim.ToString() ||
  220. extension == EAssetFileExtension.ttf.ToString() || extension == EAssetFileExtension.shader.ToString())
  221. {
  222. UnityEngine.Debug.LogWarning($"Raw file pack rule can not support file estension : {extension}");
  223. return false;
  224. }
  225. // 注意:原生文件只支持无依赖关系的资源
  226. /*
  227. string[] depends = AssetDatabase.GetDependencies(assetPath, true);
  228. if (depends.Length != 1)
  229. {
  230. UnityEngine.Debug.LogWarning($"Raw file pack rule can not support estension : {extension}");
  231. return false;
  232. }
  233. */
  234. }
  235. else
  236. {
  237. // 忽略Unity无法识别的无效文件
  238. // 注意:只对非原生文件收集器处理
  239. if (assetType == typeof(UnityEditor.DefaultAsset))
  240. {
  241. UnityEngine.Debug.LogWarning($"Cannot pack default asset : {assetPath}");
  242. return false;
  243. }
  244. }
  245. string fileExtension = System.IO.Path.GetExtension(assetPath);
  246. if (DefaultFilterRule.IsIgnoreFile(fileExtension))
  247. return false;
  248. return true;
  249. }
  250. private bool IsCollectAsset(string assetPath)
  251. {
  252. // 根据规则设置过滤资源文件
  253. IFilterRule filterRuleInstance = AssetBundleCollectorSettingData.GetFilterRuleInstance(FilterRuleName);
  254. return filterRuleInstance.IsCollectAsset(new FilterRuleData(assetPath));
  255. }
  256. private string GetAddress(CollectCommand command, AssetBundleCollectorGroup group, string assetPath)
  257. {
  258. if (command.EnableAddressable == false)
  259. return string.Empty;
  260. if (CollectorType != ECollectorType.MainAssetCollector)
  261. return string.Empty;
  262. IAddressRule addressRuleInstance = AssetBundleCollectorSettingData.GetAddressRuleInstance(AddressRuleName);
  263. string adressValue = addressRuleInstance.GetAssetAddress(new AddressRuleData(assetPath, CollectPath, group.GroupName, UserData));
  264. return adressValue;
  265. }
  266. private string GetBundleName(CollectCommand command, AssetBundleCollectorGroup group, string assetPath)
  267. {
  268. System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
  269. if (assetType == typeof(UnityEngine.Shader) || assetType == typeof(UnityEngine.ShaderVariantCollection))
  270. {
  271. // 获取着色器打包规则结果
  272. PackRuleResult packRuleResult = DefaultPackRule.CreateShadersPackRuleResult();
  273. return packRuleResult.GetMainBundleName(command.PackageName, command.UniqueBundleName);
  274. }
  275. else
  276. {
  277. // 获取其它资源打包规则结果
  278. IPackRule packRuleInstance = AssetBundleCollectorSettingData.GetPackRuleInstance(PackRuleName);
  279. PackRuleResult packRuleResult = packRuleInstance.GetPackRuleResult(new PackRuleData(assetPath, CollectPath, group.GroupName, UserData));
  280. return packRuleResult.GetMainBundleName(command.PackageName, command.UniqueBundleName);
  281. }
  282. }
  283. private List<string> GetAssetTags(AssetBundleCollectorGroup group)
  284. {
  285. List<string> tags = EditorTools.StringToStringList(group.AssetTags, ';');
  286. List<string> temper = EditorTools.StringToStringList(AssetTags, ';');
  287. tags.AddRange(temper);
  288. return tags;
  289. }
  290. private List<string> GetAllDependencies(string mainAssetPath)
  291. {
  292. string[] depends = AssetDatabase.GetDependencies(mainAssetPath, true);
  293. List<string> result = new List<string>(depends.Length);
  294. foreach (string assetPath in depends)
  295. {
  296. if (IsValidateAsset(assetPath, false))
  297. {
  298. // 注意:排除主资源对象
  299. if (assetPath != mainAssetPath)
  300. result.Add(assetPath);
  301. }
  302. }
  303. return result;
  304. }
  305. }
  306. }