BuildMapContext.cs 2.7 KB

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