AssetBundleBuildTask.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using XGame.Editor.Asset;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using XGame.Framework.Asset;
  7. namespace XGame.Editor.Build.AssetBundles
  8. {
  9. internal class AssetBundleBuildTask
  10. {
  11. private BundleContext _context;
  12. private BundleManifestsController _manifestsController;
  13. private IBuildPipeline _buildPipeline;
  14. public AssetBundleBuildTask(BundleContext context, BundleManifestsController manifestsController, IBuildPipeline buildPipeline)
  15. {
  16. _context = context;
  17. _manifestsController = manifestsController;
  18. _buildPipeline = buildPipeline;
  19. }
  20. public bool Run()
  21. {
  22. List<AssetBundleData> bundleBuilds = null;
  23. try
  24. {
  25. if (!_manifestsController.GenerateManifests())
  26. {
  27. return false;
  28. }
  29. bundleBuilds = _context.BundleDataLst;
  30. }
  31. catch (Exception ex)
  32. {
  33. Debug.LogException(ex);
  34. return false;
  35. }
  36. //Debug.Log($"ManifestBundleId: {build.TestBundleId()}");
  37. bundleBuilds.Add(CreateManifestBundle());
  38. return _buildPipeline.BuildAssetBundles(ConverToUniBundleBuilds(bundleBuilds));
  39. }
  40. private AssetBundleData CreateManifestBundle()
  41. {
  42. var assetNames = new List<string>() { PathDefine.BundleManifestPath, PathDefine.SceneAssetBundleManifestPath, PathDefine.ReferenceManifestPath, PathDefine.ProductAssetManifestPath };
  43. var addressableNames = new List<string>() { Define.ASSET_BUNDLE_MANIFEST, Define.SCENE_BUNDLE_MANIFEST, Define.ASSET_REFERENCE_MANIFEST, Define.PRODUCT_ASSET_MANIFEST };
  44. if (System.IO.File.Exists(PathDefine.BuiltInAssetManifestPath))
  45. {
  46. assetNames.Add(PathDefine.BuiltInAssetManifestPath);
  47. addressableNames.Add(Define.BUILTIN_ASSET_MANIFEST);
  48. }
  49. var build = new AssetBundleData()
  50. {
  51. assetBundleName = Define.MANIFEST_BUNDLE_NAME,
  52. assetBundleVariant = Define.BUNDLE_VARIANT,
  53. originBundleName = Define.MANIFEST_BUNDLE_NAME,
  54. assetNames = assetNames.ToArray(),
  55. addressableNames = addressableNames.ToArray()
  56. };
  57. return build;
  58. }
  59. private UnityEditor.AssetBundleBuild[] ConverToUniBundleBuilds(List<AssetBundleData> bundleBuilds)
  60. {
  61. var count = bundleBuilds.Count;
  62. var uniBuilds = new UnityEditor.AssetBundleBuild[count];
  63. for (var i = 0; i < count; i++)
  64. {
  65. uniBuilds[i] = bundleBuilds[i].ToUniBuild();
  66. }
  67. return uniBuilds;
  68. }
  69. }
  70. }