BuildAssetInfo.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace YooAsset.Editor
  6. {
  7. public class BuildAssetInfo
  8. {
  9. private bool _isAddAssetTags = false;
  10. private readonly HashSet<string> _referenceBundleNames = new HashSet<string>();
  11. /// <summary>
  12. /// 收集器类型
  13. /// </summary>
  14. public ECollectorType CollectorType { private set; get; }
  15. /// <summary>
  16. /// 资源包完整名称
  17. /// </summary>
  18. public string BundleName { private set; get; }
  19. /// <summary>
  20. /// 可寻址地址
  21. /// </summary>
  22. public string Address { private set; get; }
  23. /// <summary>
  24. /// 资源路径
  25. /// </summary>
  26. public string AssetPath { private set; get; }
  27. /// <summary>
  28. /// 资源GUID
  29. /// </summary>
  30. public string AssetGUID { private set; get; }
  31. /// <summary>
  32. /// 是否为原生资源
  33. /// </summary>
  34. public bool IsRawAsset { private set; get; }
  35. /// <summary>
  36. /// 是否为着色器资源
  37. /// </summary>
  38. public bool IsShaderAsset { private set; get; }
  39. /// <summary>
  40. /// 资源的分类标签
  41. /// </summary>
  42. public readonly List<string> AssetTags = new List<string>();
  43. /// <summary>
  44. /// 资源包的分类标签
  45. /// </summary>
  46. public readonly List<string> BundleTags = new List<string>();
  47. /// <summary>
  48. /// 依赖的所有资源
  49. /// 注意:包括零依赖资源和冗余资源(资源包名无效)
  50. /// </summary>
  51. public List<BuildAssetInfo> AllDependAssetInfos { private set; get; }
  52. public BuildAssetInfo(ECollectorType collectorType, string bundleName, string address, string assetPath, bool isRawAsset)
  53. {
  54. CollectorType = collectorType;
  55. BundleName = bundleName;
  56. Address = address;
  57. AssetPath = assetPath;
  58. IsRawAsset = isRawAsset;
  59. AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath);
  60. System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath);
  61. if (assetType == typeof(UnityEngine.Shader) || assetType == typeof(UnityEngine.ShaderVariantCollection))
  62. IsShaderAsset = true;
  63. else
  64. IsShaderAsset = false;
  65. }
  66. public BuildAssetInfo(string assetPath)
  67. {
  68. CollectorType = ECollectorType.None;
  69. Address = string.Empty;
  70. AssetPath = assetPath;
  71. IsRawAsset = false;
  72. AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath);
  73. System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath);
  74. if (assetType == typeof(UnityEngine.Shader) || assetType == typeof(UnityEngine.ShaderVariantCollection))
  75. IsShaderAsset = true;
  76. else
  77. IsShaderAsset = false;
  78. }
  79. /// <summary>
  80. /// 设置所有依赖的资源
  81. /// </summary>
  82. public void SetAllDependAssetInfos(List<BuildAssetInfo> dependAssetInfos)
  83. {
  84. if (AllDependAssetInfos != null)
  85. throw new System.Exception("Should never get here !");
  86. AllDependAssetInfos = dependAssetInfos;
  87. }
  88. /// <summary>
  89. /// 添加资源的分类标签
  90. /// 说明:原始定义的资源分类标签
  91. /// </summary>
  92. public void AddAssetTags(List<string> tags)
  93. {
  94. if (_isAddAssetTags)
  95. throw new Exception("Should never get here !");
  96. _isAddAssetTags = true;
  97. foreach (var tag in tags)
  98. {
  99. if (AssetTags.Contains(tag) == false)
  100. {
  101. AssetTags.Add(tag);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// 添加资源包的分类标签
  107. /// 说明:传染算法统计到的分类标签
  108. /// </summary>
  109. public void AddBundleTags(List<string> tags)
  110. {
  111. foreach (var tag in tags)
  112. {
  113. if (BundleTags.Contains(tag) == false)
  114. {
  115. BundleTags.Add(tag);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 资源包名是否存在
  121. /// </summary>
  122. public bool HasBundleName()
  123. {
  124. if (string.IsNullOrEmpty(BundleName))
  125. return false;
  126. else
  127. return true;
  128. }
  129. /// <summary>
  130. /// 添加关联的资源包名称
  131. /// </summary>
  132. public void AddReferenceBundleName(string bundleName)
  133. {
  134. if (string.IsNullOrEmpty(bundleName))
  135. throw new Exception("Should never get here !");
  136. if (_referenceBundleNames.Contains(bundleName) == false)
  137. _referenceBundleNames.Add(bundleName);
  138. }
  139. /// <summary>
  140. /// 计算共享资源包的完整包名
  141. /// </summary>
  142. public void CalculateShareBundleName(ISharedPackRule sharedPackRule, bool uniqueBundleName, string packageName, string shadersBundleName)
  143. {
  144. if (CollectorType != ECollectorType.None)
  145. return;
  146. if (IsRawAsset)
  147. throw new Exception("Should never get here !");
  148. if (IsShaderAsset)
  149. {
  150. BundleName = shadersBundleName;
  151. }
  152. else
  153. {
  154. if (_referenceBundleNames.Count > 1)
  155. {
  156. PackRuleResult packRuleResult = sharedPackRule.GetPackRuleResult(AssetPath);
  157. BundleName = packRuleResult.GetShareBundleName(packageName, uniqueBundleName);
  158. }
  159. else
  160. {
  161. // 注意:被引用次数小于1的资源不需要设置资源包名称
  162. BundleName = string.Empty;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 判断是否为冗余资源
  168. /// </summary>
  169. public bool IsRedundancyAsset()
  170. {
  171. if (HasBundleName())
  172. return false;
  173. return _referenceBundleNames.Count > 1;
  174. }
  175. /// <summary>
  176. /// 获取关联资源包的数量
  177. /// </summary>
  178. public int GetReferenceBundleCount()
  179. {
  180. return _referenceBundleNames.Count;
  181. }
  182. }
  183. }