1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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<PackageBundle> bundles = new HashSet<PackageBundle>();
- AssetBundlePreloadFiles assetBundlePreloadFiles = AssetBundlePreloadFiles.GetData();
- List<string> preloadFiles = new List<string>();
- 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);
- }
- }
- 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);
- Debug.Log($"preload assetPath {assetPath}");
- }
- }
- }
- // 拷贝所有首包文件
- 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}");
- }
- }
- }
- }
|