using System; using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEditor; namespace YooAsset.Editor { public class BuildMapContext : IContextObject { /// /// 参与构建的资源总数 /// 说明:包括主动收集的资源以及其依赖的所有资源 /// public int AssetFileCount; /// /// 资源包列表 /// public readonly List BundleInfos = new List(1000); /// /// 添加一个打包资源 /// public void PackAsset(BuildAssetInfo assetInfo) { string bundleName = assetInfo.GetBundleName(); if (string.IsNullOrEmpty(bundleName)) throw new Exception("Should never get here !"); if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo)) { bundleInfo.PackAsset(assetInfo); } else { BuildBundleInfo newBundleInfo = new BuildBundleInfo(bundleName); newBundleInfo.PackAsset(assetInfo); BundleInfos.Add(newBundleInfo); } } /// /// 获取所有的打包资源 /// public List GetAllAssets() { List result = new List(BundleInfos.Count); foreach (var bundleInfo in BundleInfos) { result.AddRange(bundleInfo.BuildinAssets); } return result; } /// /// 获取资源包的分类标签列表 /// public string[] GetBundleTags(string bundleName) { if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo)) { return bundleInfo.GetBundleTags(); } throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleName}"); } /// /// 获取AssetBundle内构建的资源路径列表 /// public string[] GetBuildinAssetPaths(string bundleName) { if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo)) { return bundleInfo.GetBuildinAssetPaths(); } throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleName}"); } /// /// 获取构建管线里需要的数据 /// public UnityEditor.AssetBundleBuild[] GetPipelineBuilds() { List builds = new List(BundleInfos.Count); foreach (var bundleInfo in BundleInfos) { if (bundleInfo.IsRawFile == false) builds.Add(bundleInfo.CreatePipelineBuild()); } return builds.ToArray(); } /// /// 是否包含资源包 /// public bool IsContainsBundle(string bundleName) { return TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo); } public bool TryGetBundleInfo(string bundleName, out BuildBundleInfo result) { foreach (var bundleInfo in BundleInfos) { if (bundleInfo.BundleName == bundleName) { result = bundleInfo; return true; } } result = null; return false; } } }