AssetBundleCollectorGroup.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace YooAsset.Editor
  8. {
  9. [Serializable]
  10. public class AssetBundleCollectorGroup
  11. {
  12. /// <summary>
  13. /// 分组名称
  14. /// </summary>
  15. public string GroupName = string.Empty;
  16. /// <summary>
  17. /// 分组描述
  18. /// </summary>
  19. public string GroupDesc = string.Empty;
  20. /// <summary>
  21. /// 资源分类标签
  22. /// </summary>
  23. public string AssetTags = string.Empty;
  24. /// <summary>
  25. /// 分组激活规则
  26. /// </summary>
  27. public string ActiveRuleName = nameof(EnableGroup);
  28. /// <summary>
  29. /// 分组的收集器列表
  30. /// </summary>
  31. public List<AssetBundleCollector> Collectors = new List<AssetBundleCollector>();
  32. /// <summary>
  33. /// 检测配置错误
  34. /// </summary>
  35. public void CheckConfigError()
  36. {
  37. if (AssetBundleCollectorSettingData.HasActiveRuleName(ActiveRuleName) == false)
  38. throw new Exception($"Invalid {nameof(IActiveRule)} class type : {ActiveRuleName} in group : {GroupName}");
  39. foreach (var collector in Collectors)
  40. {
  41. collector.CheckConfigError();
  42. }
  43. }
  44. /// <summary>
  45. /// 修复配置错误
  46. /// </summary>
  47. public bool FixConfigError()
  48. {
  49. bool isFixed = false;
  50. foreach (var collector in Collectors)
  51. {
  52. if (collector.FixConfigError())
  53. {
  54. isFixed = true;
  55. }
  56. }
  57. return isFixed;
  58. }
  59. /// <summary>
  60. /// 获取打包收集的资源文件
  61. /// </summary>
  62. public List<CollectAssetInfo> GetAllCollectAssets(CollectCommand command)
  63. {
  64. Dictionary<string, CollectAssetInfo> result = new Dictionary<string, CollectAssetInfo>(10000);
  65. // 检测分组是否激活
  66. IActiveRule activeRule = AssetBundleCollectorSettingData.GetActiveRuleInstance(ActiveRuleName);
  67. if (activeRule.IsActiveGroup() == false)
  68. {
  69. return new List<CollectAssetInfo>();
  70. }
  71. // 收集打包资源
  72. foreach (var collector in Collectors)
  73. {
  74. var temper = collector.GetAllCollectAssets(command, this);
  75. foreach (var assetInfo in temper)
  76. {
  77. if (result.ContainsKey(assetInfo.AssetPath) == false)
  78. result.Add(assetInfo.AssetPath, assetInfo);
  79. else
  80. throw new Exception($"The collecting asset file is existed : {assetInfo.AssetPath} in group : {GroupName}");
  81. }
  82. }
  83. // 检测可寻址地址是否重复
  84. if (command.EnableAddressable)
  85. {
  86. var addressTemper = new Dictionary<string, string>();
  87. foreach (var collectInfoPair in result)
  88. {
  89. if (collectInfoPair.Value.CollectorType == ECollectorType.MainAssetCollector)
  90. {
  91. string address = collectInfoPair.Value.Address;
  92. string assetPath = collectInfoPair.Value.AssetPath;
  93. if (addressTemper.TryGetValue(address, out var existed) == false)
  94. addressTemper.Add(address, assetPath);
  95. else
  96. throw new Exception($"The address is existed : {address} in group : {GroupName} \nAssetPath:\n {existed}\n {assetPath}");
  97. }
  98. }
  99. }
  100. // 返回列表
  101. return result.Values.ToList();
  102. }
  103. }
  104. }