PresetAssetHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.GetDressUpLayerSpriteResPath(dressUpCfg, 1);
  53. TryAdd(assetPath, manifest, bundles);
  54. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 2);
  55. TryAdd(assetPath, manifest, bundles);
  56. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 3);
  57. TryAdd(assetPath, manifest, bundles);
  58. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  59. TryAdd(assetPath, manifest, bundles);
  60. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  61. TryAdd(assetPath, manifest, bundles);
  62. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  63. TryAdd(assetPath, manifest, bundles);
  64. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1);
  65. TryAdd(assetPath, manifest, bundles);
  66. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2);
  67. TryAdd(assetPath, manifest, bundles);
  68. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3);
  69. TryAdd(assetPath, manifest, bundles);
  70. }
  71. }
  72. string root = $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  73. //清空
  74. EditorTools.ClearFolder(root);
  75. // 拷贝指定动态加载文件
  76. foreach (var packageBundle in bundles)
  77. {
  78. string destPath = $"{root}/{packageBundle.FileName}";
  79. //...... //拷贝文件
  80. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  81. EditorTools.CopyFile(sourcePath, destPath, true);
  82. //Debug.Log($"sourcePath { sourcePath}");
  83. Debug.Log($"destPath { destPath}");
  84. }
  85. //拷贝指定标签的文件
  86. string[] tags = { "preload"};
  87. foreach (var packageBundle in manifest.BundleList)
  88. {
  89. if (packageBundle.HasTag(tags) == false)
  90. continue;
  91. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  92. string destPath = $"{root}/{packageBundle.FileName}";
  93. EditorTools.CopyFile(sourcePath, destPath, true);
  94. Debug.Log($"destPath { destPath}");
  95. }
  96. }
  97. private static void TryAdd(string assetPath, PackageManifest manifest, HashSet<PackageBundle> bundles)
  98. {
  99. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  100. {
  101. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  102. if (bundles.Contains(packageBundle) == false)
  103. {
  104. bundles.Add(packageBundle);
  105. Debug.Log($"preload assetPath {assetPath}");
  106. }
  107. }
  108. }
  109. }
  110. }