PreloadAssetHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. foreach (var assetPath in assetBundlePreloadFiles.PreloadFiles)
  30. {
  31. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  32. {
  33. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  34. if (bundles.Contains(packageBundle) == false)
  35. bundles.Add(packageBundle);
  36. }
  37. }
  38. // 拷贝所有首包文件
  39. string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  40. foreach (var packageBundle in bundles)
  41. {
  42. string destPath = $"{root}/{packageBundle.FileName}";
  43. //...... //拷贝文件
  44. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  45. File.Copy(sourcePath, destPath, true);
  46. Debug.Log($"sourcePath { sourcePath}");
  47. Debug.Log($"destPath { destPath}");
  48. }
  49. }
  50. }
  51. }