using System.Collections.Generic; using YooAsset.Editor; using YooAsset; using GFGGame; using UnityEditor; using System.IO; using UnityEngine; namespace GFGEditor { public class PreloadAssetHelper { public static void CopyPreloadAssets() { // 等待资源构建流程完成 //...... string selectedFile = EditorUtility.OpenFilePanel("Select a file", "", ""); if(string.IsNullOrEmpty(selectedFile)) { return; } int t = selectedFile.LastIndexOf("/"); string dirPath = selectedFile.Substring(0, t); // 加载构建成功的资源清单对象 byte[] manifestBytes = FileUtility.ReadAllBytes(selectedFile); PackageManifest manifest = ManifestTools.DeserializeFromBinary(manifestBytes); // 查找所有需要打进首包资源的依赖AB HashSet bundles = new HashSet(); AssetBundlePreloadFiles assetBundlePreloadFiles = AssetBundlePreloadFiles.GetData(); List preloadFiles = new List(); preloadFiles.AddRange(assetBundlePreloadFiles.PreloadFiles); SqliteController.Instance.Init(false, null); StoryDialogCfg[] dataArray = StoryDialogCfgArray.Instance.dataArray; foreach(var cfg in dataArray) { if(!string.IsNullOrEmpty(cfg.bgRes)) { var resPath = ResPathUtil.GetSceneBgPath(cfg.bgRes); preloadFiles.Add(resPath); Debug.Log($"preload StoryDialogCfg cfg.bgRes {cfg.bgRes} resPath {resPath}"); } } foreach (var assetPath in preloadFiles) { if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset)) { var packageBundle = manifest.BundleList[packageAsset.BundleID]; if (bundles.Contains(packageBundle) == false) bundles.Add(packageBundle); } } // 拷贝所有首包文件 string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}"; foreach (var packageBundle in bundles) { string destPath = $"{root}/{packageBundle.FileName}"; //...... //拷贝文件 string sourcePath = $"{dirPath}/{packageBundle.FileName}"; File.Copy(sourcePath, destPath, true); Debug.Log($"sourcePath { sourcePath}"); Debug.Log($"destPath { destPath}"); } } } }