PreloadAssetHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Generic;
  2. using YooAsset.Editor;
  3. using YooAsset;
  4. using GFGGame;
  5. using UnityEditor;
  6. using System.IO;
  7. using UnityEngine;
  8. namespace GFGEditor
  9. {
  10. public class PreloadAssetHelper
  11. {
  12. public static void CopyPreloadAssets()
  13. {
  14. // 等待资源构建流程完成
  15. //......
  16. string selectedFile = EditorUtility.OpenFilePanel("Select a file", "", "");
  17. if(string.IsNullOrEmpty(selectedFile))
  18. {
  19. return;
  20. }
  21. int t = selectedFile.LastIndexOf("/");
  22. string dirPath = selectedFile.Substring(0, t);
  23. // 加载构建成功的资源清单对象
  24. byte[] manifestBytes = FileUtility.ReadAllBytes(selectedFile);
  25. PackageManifest manifest = ManifestTools.DeserializeFromBinary(manifestBytes);
  26. // 查找所有需要打进首包资源的依赖AB
  27. HashSet<PackageBundle> bundles = new HashSet<PackageBundle>();
  28. AssetBundlePreloadFiles assetBundlePreloadFiles = AssetBundlePreloadFiles.GetData();
  29. List<string> preloadFiles = new List<string>();
  30. preloadFiles.AddRange(assetBundlePreloadFiles.PreloadFiles);
  31. var dataArray = StoryDialogCfgArray.Instance.dataArray;
  32. foreach(var cfg in dataArray)
  33. {
  34. if(!string.IsNullOrEmpty(cfg.bgRes))
  35. {
  36. var resPath = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  37. preloadFiles.Add(resPath);
  38. }
  39. }
  40. foreach (var assetPath in assetBundlePreloadFiles.PreloadFiles)
  41. {
  42. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  43. {
  44. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  45. if (bundles.Contains(packageBundle) == false)
  46. bundles.Add(packageBundle);
  47. }
  48. }
  49. // 拷贝所有首包文件
  50. string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  51. foreach (var packageBundle in bundles)
  52. {
  53. string destPath = $"{root}/{packageBundle.FileName}";
  54. //...... //拷贝文件
  55. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  56. File.Copy(sourcePath, destPath, true);
  57. Debug.Log($"sourcePath { sourcePath}");
  58. Debug.Log($"destPath { destPath}");
  59. }
  60. }
  61. }
  62. }