BuildAssetInfo.cs 5.1 KB

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