PresetAssetHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections.Generic;
  2. using YooAsset.Editor;
  3. using YooAsset;
  4. using GFGGame;
  5. using UnityEditor;
  6. using System.IO;
  7. using cfg.GfgCfg;
  8. using UnityEngine;
  9. namespace GFGEditor
  10. {
  11. public class PresetAssetHelper
  12. {
  13. public static void CopyPresetAssets()
  14. {
  15. // 等待资源构建流程完成
  16. //......
  17. //选择清单文件
  18. string selectedFile = EditorUtility.OpenFilePanel("Select a file", "", "");
  19. if (string.IsNullOrEmpty(selectedFile))
  20. {
  21. return;
  22. }
  23. int t = selectedFile.LastIndexOf("/");
  24. string dirPath = selectedFile.Substring(0, t);
  25. //生成预制资源列表
  26. AssetBundlePreloadFiles assetBundlePreloadFiles = AssetBundlePreloadFiles.GetData();
  27. List<string> preloadFiles = new List<string>();
  28. preloadFiles.AddRange(assetBundlePreloadFiles.PreloadFiles);
  29. //SqliteController.Instance.Init(false, null);
  30. // List<StoryDialogCfg> dataArray = CommonDataManager.Tables.TblStoryDialogCfg.DataList;
  31. // foreach (var cfg in dataArray)
  32. // {
  33. // if (!string.IsNullOrEmpty(cfg.BgRes))
  34. // {
  35. // var resPath = ResPathUtil.GetSceneBgPath(cfg.BgRes);
  36. // preloadFiles.Add(resPath);
  37. // }
  38. // }
  39. // 加载构建成功的资源清单对象
  40. byte[] manifestBytes = FileUtility.ReadAllBytes(selectedFile);
  41. PackageManifest manifest = ManifestTools.DeserializeFromBinary(manifestBytes);
  42. // 查找所有需要打进首包资源的依赖AB
  43. HashSet<PackageBundle> bundles = new HashSet<PackageBundle>();
  44. foreach (var assetPath in preloadFiles)
  45. {
  46. TryAdd(assetPath, manifest, bundles);
  47. }
  48. // List<ItemCfg> dressUpItemlist =
  49. // CommonDataManager.Tables.TblItemCfg.GetGroup1ByItemType(ConstItemType.DRESS_UP);
  50. // foreach (var dressUpCfg in dressUpItemlist)
  51. // {
  52. // if (dressUpCfg.LoadType > 0)
  53. // {
  54. // string assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 1);
  55. // TryAdd(assetPath, manifest, bundles);
  56. // assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 2);
  57. // TryAdd(assetPath, manifest, bundles);
  58. // assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 3);
  59. // TryAdd(assetPath, manifest, bundles);
  60. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  61. // TryAdd(assetPath, manifest, bundles);
  62. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  63. // TryAdd(assetPath, manifest, bundles);
  64. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  65. // TryAdd(assetPath, manifest, bundles);
  66. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, false);
  67. // TryAdd(assetPath, manifest, bundles);
  68. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, false);
  69. // TryAdd(assetPath, manifest, bundles);
  70. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, false);
  71. // TryAdd(assetPath, manifest, bundles);
  72. // TryAdd(assetPath, manifest, bundles);
  73. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, true);
  74. // TryAdd(assetPath, manifest, bundles);
  75. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, true);
  76. // TryAdd(assetPath, manifest, bundles);
  77. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, true);
  78. // TryAdd(assetPath, manifest, bundles);
  79. // }
  80. // }
  81. string root =
  82. $"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  83. //清空
  84. EditorTools.ClearFolder(root);
  85. // 拷贝指定动态加载文件
  86. foreach (var packageBundle in bundles)
  87. {
  88. string destPath = $"{root}/{packageBundle.FileName}";
  89. //...... //拷贝文件
  90. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  91. EditorTools.CopyFile(sourcePath, destPath, true);
  92. //Debug.Log($"sourcePath { sourcePath}");
  93. Debug.Log($"destPath {destPath}");
  94. }
  95. //拷贝指定标签的文件
  96. string[] tags = { "preload" };
  97. foreach (var packageBundle in manifest.BundleList)
  98. {
  99. if (packageBundle.HasTag(tags) == false)
  100. continue;
  101. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  102. string destPath = $"{root}/{packageBundle.FileName}";
  103. EditorTools.CopyFile(sourcePath, destPath, true);
  104. Debug.Log($"destPath {destPath}");
  105. }
  106. }
  107. private static void TryAdd(string assetPath, PackageManifest manifest, HashSet<PackageBundle> bundles)
  108. {
  109. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  110. {
  111. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  112. if (bundles.Contains(packageBundle) == false)
  113. {
  114. bundles.Add(packageBundle);
  115. Debug.Log($"preload assetPath {assetPath}");
  116. }
  117. }
  118. }
  119. }
  120. }