BuildBundleInfo.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. namespace YooAsset.Editor
  7. {
  8. public class BuildBundleInfo
  9. {
  10. /// <summary>
  11. /// 资源包名称
  12. /// </summary>
  13. public string BundleName { private set; get; }
  14. /// <summary>
  15. /// 参与构建的资源列表
  16. /// 注意:不包含零依赖资源
  17. /// </summary>
  18. public readonly List<BuildAssetInfo> BuildinAssets = new List<BuildAssetInfo>();
  19. /// <summary>
  20. /// 是否为原生文件
  21. /// </summary>
  22. public bool IsRawFile
  23. {
  24. get
  25. {
  26. foreach (var asset in BuildinAssets)
  27. {
  28. if (asset.IsRawAsset)
  29. return true;
  30. }
  31. return false;
  32. }
  33. }
  34. /// <summary>
  35. /// 构建内容哈希值
  36. /// </summary>
  37. public string ContentHash { set; get; } = "00000000000000000000000000000000"; //32位
  38. public BuildBundleInfo(string bundleName)
  39. {
  40. BundleName = bundleName;
  41. }
  42. /// <summary>
  43. /// 添加一个打包资源
  44. /// </summary>
  45. public void PackAsset(BuildAssetInfo assetInfo)
  46. {
  47. if (IsContainsAsset(assetInfo.AssetPath))
  48. throw new System.Exception($"Asset is existed : {assetInfo.AssetPath}");
  49. BuildinAssets.Add(assetInfo);
  50. }
  51. /// <summary>
  52. /// 是否包含指定资源
  53. /// </summary>
  54. public bool IsContainsAsset(string assetPath)
  55. {
  56. foreach (var assetInfo in BuildinAssets)
  57. {
  58. if (assetInfo.AssetPath == assetPath)
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. /// <summary>
  66. /// 获取资源包的分类标签列表
  67. /// </summary>
  68. public string[] GetBundleTags()
  69. {
  70. List<string> result = new List<string>(BuildinAssets.Count);
  71. foreach (var assetInfo in BuildinAssets)
  72. {
  73. foreach (var assetTag in assetInfo.BundleTags)
  74. {
  75. if (result.Contains(assetTag) == false)
  76. result.Add(assetTag);
  77. }
  78. }
  79. return result.ToArray();
  80. }
  81. /// <summary>
  82. /// 获取文件的扩展名
  83. /// </summary>
  84. public string GetAppendExtension()
  85. {
  86. return System.IO.Path.GetExtension(BundleName);
  87. }
  88. /// <summary>
  89. /// 获取构建的资源路径列表
  90. /// </summary>
  91. public string[] GetBuildinAssetPaths()
  92. {
  93. return BuildinAssets.Select(t => t.AssetPath).ToArray();
  94. }
  95. /// <summary>
  96. /// 获取所有写入补丁清单的资源
  97. /// </summary>
  98. public BuildAssetInfo[] GetAllPatchAssetInfos()
  99. {
  100. return BuildinAssets.Where(t => t.CollectorType == ECollectorType.MainAssetCollector).ToArray();
  101. }
  102. /// <summary>
  103. /// 创建AssetBundleBuild类
  104. /// </summary>
  105. public UnityEditor.AssetBundleBuild CreatePipelineBuild()
  106. {
  107. // 注意:我们不在支持AssetBundle的变种机制
  108. AssetBundleBuild build = new AssetBundleBuild();
  109. build.assetBundleName = BundleName;
  110. build.assetBundleVariant = string.Empty;
  111. build.assetNames = GetBuildinAssetPaths();
  112. return build;
  113. }
  114. }
  115. }