BuildMapContext.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 BuildMapContext : IContextObject
  9. {
  10. /// <summary>
  11. /// 参与构建的资源总数
  12. /// 说明:包括主动收集的资源以及其依赖的所有资源
  13. /// </summary>
  14. public int AssetFileCount;
  15. /// <summary>
  16. /// 资源包列表
  17. /// </summary>
  18. public readonly List<BuildBundleInfo> BundleInfos = new List<BuildBundleInfo>(1000);
  19. /// <summary>
  20. /// 添加一个打包资源
  21. /// </summary>
  22. public void PackAsset(BuildAssetInfo assetInfo)
  23. {
  24. string bundleName = assetInfo.GetBundleName();
  25. if (string.IsNullOrEmpty(bundleName))
  26. throw new Exception("Should never get here !");
  27. if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
  28. {
  29. bundleInfo.PackAsset(assetInfo);
  30. }
  31. else
  32. {
  33. BuildBundleInfo newBundleInfo = new BuildBundleInfo(bundleName);
  34. newBundleInfo.PackAsset(assetInfo);
  35. BundleInfos.Add(newBundleInfo);
  36. }
  37. }
  38. /// <summary>
  39. /// 获取所有的打包资源
  40. /// </summary>
  41. public List<BuildAssetInfo> GetAllAssets()
  42. {
  43. List<BuildAssetInfo> result = new List<BuildAssetInfo>(BundleInfos.Count);
  44. foreach (var bundleInfo in BundleInfos)
  45. {
  46. result.AddRange(bundleInfo.BuildinAssets);
  47. }
  48. return result;
  49. }
  50. /// <summary>
  51. /// 获取资源包的分类标签列表
  52. /// </summary>
  53. public string[] GetBundleTags(string bundleName)
  54. {
  55. if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
  56. {
  57. return bundleInfo.GetBundleTags();
  58. }
  59. throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleName}");
  60. }
  61. /// <summary>
  62. /// 获取AssetBundle内构建的资源路径列表
  63. /// </summary>
  64. public string[] GetBuildinAssetPaths(string bundleName)
  65. {
  66. if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
  67. {
  68. return bundleInfo.GetBuildinAssetPaths();
  69. }
  70. throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleName}");
  71. }
  72. /// <summary>
  73. /// 获取构建管线里需要的数据
  74. /// </summary>
  75. public UnityEditor.AssetBundleBuild[] GetPipelineBuilds()
  76. {
  77. List<UnityEditor.AssetBundleBuild> builds = new List<UnityEditor.AssetBundleBuild>(BundleInfos.Count);
  78. foreach (var bundleInfo in BundleInfos)
  79. {
  80. if (bundleInfo.IsRawFile == false)
  81. builds.Add(bundleInfo.CreatePipelineBuild());
  82. }
  83. return builds.ToArray();
  84. }
  85. /// <summary>
  86. /// 是否包含资源包
  87. /// </summary>
  88. public bool IsContainsBundle(string bundleName)
  89. {
  90. return TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo);
  91. }
  92. public bool TryGetBundleInfo(string bundleName, out BuildBundleInfo result)
  93. {
  94. foreach (var bundleInfo in BundleInfos)
  95. {
  96. if (bundleInfo.BundleName == bundleName)
  97. {
  98. result = bundleInfo;
  99. return true;
  100. }
  101. }
  102. result = null;
  103. return false;
  104. }
  105. }
  106. }