PresetAssetHelper.cs 5.6 KB

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