BuildMapContext.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /// </summary>
  13. private readonly Dictionary<string, BuildBundleInfo> _bundleInfoDic = new Dictionary<string, BuildBundleInfo>(10000);
  14. /// <summary>
  15. /// 未被依赖的资源列表
  16. /// </summary>
  17. public readonly List<ReportIndependAsset> IndependAssets = new List<ReportIndependAsset>(1000);
  18. /// <summary>
  19. /// 参与构建的资源总数
  20. /// 说明:包括主动收集的资源以及其依赖的所有资源
  21. /// </summary>
  22. public int AssetFileCount;
  23. /// <summary>
  24. /// 资源收集命令
  25. /// </summary>
  26. public CollectCommand Command { set; get; }
  27. /// <summary>
  28. /// 资源包信息列表
  29. /// </summary>
  30. public Dictionary<string, BuildBundleInfo>.ValueCollection Collection
  31. {
  32. get
  33. {
  34. return _bundleInfoDic.Values;
  35. }
  36. }
  37. /// <summary>
  38. /// 添加一个打包资源
  39. /// </summary>
  40. public void PackAsset(BuildAssetInfo assetInfo)
  41. {
  42. string bundleName = assetInfo.BundleName;
  43. if (string.IsNullOrEmpty(bundleName))
  44. throw new Exception("Should never get here !");
  45. if (_bundleInfoDic.TryGetValue(bundleName, out BuildBundleInfo bundleInfo))
  46. {
  47. bundleInfo.PackAsset(assetInfo);
  48. }
  49. else
  50. {
  51. BuildBundleInfo newBundleInfo = new BuildBundleInfo(bundleName);
  52. newBundleInfo.PackAsset(assetInfo);
  53. _bundleInfoDic.Add(bundleName, newBundleInfo);
  54. }
  55. }
  56. /// <summary>
  57. /// 是否包含资源包
  58. /// </summary>
  59. public bool IsContainsBundle(string bundleName)
  60. {
  61. return _bundleInfoDic.ContainsKey(bundleName);
  62. }
  63. /// <summary>
  64. /// 获取资源包信息,如果没找到返回NULL
  65. /// </summary>
  66. public BuildBundleInfo GetBundleInfo(string bundleName)
  67. {
  68. if (_bundleInfoDic.TryGetValue(bundleName, out BuildBundleInfo result))
  69. {
  70. return result;
  71. }
  72. throw new Exception($"Should never get here ! Not found bundle : {bundleName}");
  73. }
  74. /// <summary>
  75. /// 获取构建管线里需要的数据
  76. /// </summary>
  77. public UnityEditor.AssetBundleBuild[] GetPipelineBuilds()
  78. {
  79. List<UnityEditor.AssetBundleBuild> builds = new List<UnityEditor.AssetBundleBuild>(_bundleInfoDic.Count);
  80. foreach (var bundleInfo in _bundleInfoDic.Values)
  81. {
  82. builds.Add(bundleInfo.CreatePipelineBuild());
  83. }
  84. return builds.ToArray();
  85. }
  86. /// <summary>
  87. /// 创建着色器信息类
  88. /// </summary>
  89. public void CreateShadersBundleInfo(string shadersBundleName)
  90. {
  91. if (IsContainsBundle(shadersBundleName) == false)
  92. {
  93. var shaderBundleInfo = new BuildBundleInfo(shadersBundleName);
  94. _bundleInfoDic.Add(shadersBundleName, shaderBundleInfo);
  95. }
  96. }
  97. }
  98. }