PresetAssetHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. PackageManifest manifest = ManifestTools.DeserializeFromBinary(manifestBytes, null);
  43. // 查找所有需要打进首包资源的依赖AB
  44. HashSet<PackageBundle> bundles = new HashSet<PackageBundle>();
  45. foreach (var assetPath in preloadFiles)
  46. {
  47. TryAdd(assetPath, manifest, bundles);
  48. }
  49. // List<ItemCfg> dressUpItemlist =
  50. // CommonDataManager.Tables.TblItemCfg.GetGroup1ByItemType(ConstItemType.DRESS_UP);
  51. // foreach (var dressUpCfg in dressUpItemlist)
  52. // {
  53. // if (dressUpCfg.LoadType > 0)
  54. // {
  55. // string assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 1);
  56. // TryAdd(assetPath, manifest, bundles);
  57. // assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 2);
  58. // TryAdd(assetPath, manifest, bundles);
  59. // assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 3);
  60. // TryAdd(assetPath, manifest, bundles);
  61. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  62. // TryAdd(assetPath, manifest, bundles);
  63. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  64. // TryAdd(assetPath, manifest, bundles);
  65. // assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  66. // TryAdd(assetPath, manifest, bundles);
  67. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, false);
  68. // TryAdd(assetPath, manifest, bundles);
  69. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, false);
  70. // TryAdd(assetPath, manifest, bundles);
  71. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, false);
  72. // TryAdd(assetPath, manifest, bundles);
  73. // TryAdd(assetPath, manifest, bundles);
  74. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, true);
  75. // TryAdd(assetPath, manifest, bundles);
  76. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, true);
  77. // TryAdd(assetPath, manifest, bundles);
  78. // assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, true);
  79. // TryAdd(assetPath, manifest, bundles);
  80. // }
  81. // }
  82. string root =
  83. //$"{AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot()}/{VersionController.DefaultPackage}";
  84. $"{AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()}/{VersionController.DefaultPackage}";
  85. //清空
  86. EditorTools.ClearFolder(root);
  87. // 拷贝指定动态加载文件
  88. foreach (var packageBundle in bundles)
  89. {
  90. string destPath = $"{root}/{packageBundle.FileName}";
  91. //...... //拷贝文件
  92. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  93. EditorTools.CopyFile(sourcePath, destPath, true);
  94. //Debug.Log($"sourcePath { sourcePath}");
  95. Debug.Log($"destPath {destPath}");
  96. }
  97. //拷贝指定标签的文件
  98. string[] tags = { "preload" };
  99. foreach (var packageBundle in manifest.BundleList)
  100. {
  101. if (packageBundle.HasTag(tags) == false)
  102. continue;
  103. string sourcePath = $"{dirPath}/{packageBundle.FileName}";
  104. string destPath = $"{root}/{packageBundle.FileName}";
  105. EditorTools.CopyFile(sourcePath, destPath, true);
  106. Debug.Log($"destPath {destPath}");
  107. }
  108. }
  109. private static void TryAdd(string assetPath, PackageManifest manifest, HashSet<PackageBundle> bundles)
  110. {
  111. if (manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  112. {
  113. var packageBundle = manifest.BundleList[packageAsset.BundleID];
  114. if (bundles.Contains(packageBundle) == false)
  115. {
  116. bundles.Add(packageBundle);
  117. Debug.Log($"preload assetPath {assetPath}");
  118. }
  119. }
  120. }
  121. }
  122. }