PresetAssetHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 PresetAssetHelper
  11. {
  12. public static void CopyPresetAssets()
  13. {
  14. // 等待资源构建流程完成
  15. //......
  16. //选择清单文件
  17. string selectedFile = EditorUtility.OpenFilePanel("Select a file", "", "");
  18. if(string.IsNullOrEmpty(selectedFile))
  19. {
  20. return;
  21. }
  22. int t = selectedFile.LastIndexOf("/");
  23. string dirPath = selectedFile.Substring(0, t);
  24. //生成预制资源列表
  25. AssetBundlePreloadFiles assetBundlePreloadFiles = AssetBundlePreloadFiles.GetData();
  26. List<string> preloadFiles = new List<string>();
  27. preloadFiles.AddRange(assetBundlePreloadFiles.PreloadFiles);
  28. SqliteController.Instance.Init(false, null);
  29. StoryDialogCfg[] dataArray = StoryDialogCfgArray.Instance.dataArray;
  30. foreach (var cfg in dataArray)
  31. {
  32. if (!string.IsNullOrEmpty(cfg.bgRes))
  33. {
  34. var resPath = ResPathUtil.GetSceneBgPath(cfg.bgRes);
  35. preloadFiles.Add(resPath);
  36. }
  37. }
  38. // 加载构建成功的资源清单对象
  39. byte[] manifestBytes = FileUtility.ReadAllBytes(selectedFile);
  40. PackageManifest manifest = ManifestTools.DeserializeFromBinary(manifestBytes);
  41. // 查找所有需要打进首包资源的依赖AB
  42. HashSet<PackageBundle> bundles = new HashSet<PackageBundle>();
  43. foreach (var assetPath in preloadFiles)
  44. {
  45. TryAdd(assetPath, manifest, bundles);
  46. }
  47. List<ItemCfg> dressUpItemlist = ItemCfgArray.Instance.GetCfgsByitemType(ConstItemType.DRESS_UP);
  48. foreach(var dressUpCfg in dressUpItemlist)
  49. {
  50. if(dressUpCfg.loadType > 0)
  51. {
  52. string assetPath = ResPathUtil.GetDressUpItemLayerRes(dressUpCfg, 1);
  53. TryAdd(assetPath, manifest, bundles);
  54. assetPath = ResPathUtil.GetDressUpItemLayerRes(dressUpCfg, 2);
  55. TryAdd(assetPath, manifest, bundles);
  56. assetPath = ResPathUtil.GetDressUpItemLayerRes(dressUpCfg, 3);
  57. TryAdd(assetPath, manifest, bundles);
  58. assetPath = ResPathUtil.GetDressUpAnimationPath(dressUpCfg.res);
  59. TryAdd(assetPath, manifest, bundles);
  60. assetPath = ResPathUtil.GetDressUpEffectPath(dressUpCfg.res);
  61. TryAdd(assetPath, manifest, bundles);
  62. }
  63. }
  64. string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  65. // 拷贝指定动态加载文件
  66. foreach (var packageBundle in bundles)
  67. {
  68. string destPath = $"{root}/{packageBundle.FileName}";
  69. //...... //拷贝文件
  70. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  71. EditorTools.CopyFile(sourcePath, destPath, true);
  72. //Debug.Log($"sourcePath { sourcePath}");
  73. Debug.Log($"destPath { destPath}");
  74. }
  75. //拷贝指定标签的文件
  76. string[] tags = { "preload"};
  77. foreach (var packageBundle in manifest.BundleList)
  78. {
  79. if (packageBundle.HasTag(tags) == false)
  80. continue;
  81. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  82. string destPath = $"{root}/{packageBundle.FileName}";
  83. EditorTools.CopyFile(sourcePath, destPath, true);
  84. Debug.Log($"destPath { destPath}");
  85. }
  86. }
  87. private static void TryAdd(string assetPath, PackageManifest manifest, HashSet<PackageBundle> bundles)
  88. {
  89. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  90. {
  91. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  92. if (bundles.Contains(packageBundle) == false)
  93. {
  94. bundles.Add(packageBundle);
  95. Debug.Log($"preload assetPath {assetPath}");
  96. }
  97. }
  98. }
  99. }
  100. }