PresetAssetHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. EditorTools.ClearFolder(root);
  67. // 拷贝指定动态加载文件
  68. foreach (var packageBundle in bundles)
  69. {
  70. string destPath = $"{root}/{packageBundle.FileName}";
  71. //...... //拷贝文件
  72. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  73. EditorTools.CopyFile(sourcePath, destPath, true);
  74. //Debug.Log($"sourcePath { sourcePath}");
  75. Debug.Log($"destPath { destPath}");
  76. }
  77. //拷贝指定标签的文件
  78. string[] tags = { "preload"};
  79. foreach (var packageBundle in manifest.BundleList)
  80. {
  81. if (packageBundle.HasTag(tags) == false)
  82. continue;
  83. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  84. string destPath = $"{root}/{packageBundle.FileName}";
  85. EditorTools.CopyFile(sourcePath, destPath, true);
  86. Debug.Log($"destPath { destPath}");
  87. }
  88. }
  89. private static void TryAdd(string assetPath, PackageManifest manifest, HashSet<PackageBundle> bundles)
  90. {
  91. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  92. {
  93. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  94. if (bundles.Contains(packageBundle) == false)
  95. {
  96. bundles.Add(packageBundle);
  97. Debug.Log($"preload assetPath {assetPath}");
  98. }
  99. }
  100. }
  101. }
  102. }