PreloadAssetHelper.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. SqliteController.Instance.Init(false, null);
  32. StoryDialogCfg[] dataArray = StoryDialogCfgArray.Instance.dataArray;
  33. foreach(var cfg in dataArray)
  34. {
  35. if(!string.IsNullOrEmpty(cfg.bgRes))
  36. {
  37. var resPath = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  38. preloadFiles.Add(resPath);
  39. }
  40. }
  41. foreach (var assetPath in preloadFiles)
  42. {
  43. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  44. {
  45. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  46. if (bundles.Contains(packageBundle) == false)
  47. {
  48. bundles.Add(packageBundle);
  49. Debug.Log($"preload assetPath {assetPath}");
  50. }
  51. }
  52. }
  53. // 拷贝所有首包文件
  54. string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  55. foreach (var packageBundle in bundles)
  56. {
  57. string destPath = $"{root}/{packageBundle.FileName}";
  58. //...... //拷贝文件
  59. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  60. File.Copy(sourcePath, destPath, true);
  61. //Debug.Log($"sourcePath { sourcePath}");
  62. Debug.Log($"destPath { destPath}");
  63. }
  64. }
  65. }
  66. }