SbpBuildPipeline.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using XGame.Editor.Asset;
  2. using System;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEditor.Build.Pipeline;
  6. using UnityEngine;
  7. using UnityEngine.Build.Pipeline;
  8. namespace XGame.Editor.Build.AssetBundles
  9. {
  10. public class SbpBuildPipeline : IBuildPipeline
  11. {
  12. public bool BuildAssetBundles(UnityEditor.AssetBundleBuild[] builds)
  13. {
  14. var outputPath = PathDefine.AssetBundleOutputPath; // KFADF.FileUtil.WorkPath + "/AssetBundles";
  15. if (!Directory.Exists(outputPath))
  16. {
  17. Directory.CreateDirectory(outputPath);
  18. }
  19. //LZ4压缩 | 报错中断
  20. //剔除文件头的Unity版本号信息 BuildAssetBundleOptions.AssetBundleStripUnityVersion
  21. var bundleOptions = BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode;
  22. // bool shouldCheckODR = EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS;
  23. //#if UNITY_TVOS
  24. // shouldCheckODR |= EditorUserBuildSettings.activeBuildTarget == BuildTarget.tvOS;
  25. //#endif
  26. // if (shouldCheckODR)
  27. // {
  28. //#if ENABLE_IOS_ON_DEMAND_RESOURCES
  29. // if (PlayerSettings.iOS.useOnDemandResources)
  30. // bundleOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
  31. //#endif
  32. //#if ENABLE_IOS_APP_SLICING
  33. // bundleOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
  34. //#endif
  35. // }
  36. return BuildAssetBundles(outputPath, builds, bundleOptions, EditorUserBuildSettings.activeBuildTarget);
  37. }
  38. public bool BuildAssetBundles(string outputPath, UnityEditor.AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform)
  39. {
  40. BuildLog.Log($"SbpBuildPipeline. Target: {EditorUserBuildSettings.activeBuildTarget} Time: {DateTime.Now}");
  41. var manifest = CompatibilityBuildPipeline.BuildAssetBundles(outputPath, builds, assetBundleOptions, targetPlatform);
  42. if (manifest != null)
  43. {
  44. BuildLog.Log($"SbpBuildPipeline success. Time: {DateTime.Now}");
  45. PostBuildAssetBundles(manifest, outputPath);
  46. return true;
  47. }
  48. else
  49. {
  50. BuildLog.Error($"SbpBuildPipeline failed. Time: {DateTime.Now}");
  51. }
  52. return false;
  53. }
  54. /// <summary>
  55. /// SBP的ab没有后缀,手动补上后缀,并且没有了 *.bunle.manifest文件,crc值需要manifest里获取
  56. /// </summary>
  57. /// <param name="manifest"></param>
  58. private void PostBuildAssetBundles(CompatibilityAssetBundleManifest manifest, string outputPath)
  59. {
  60. var bundleNames = manifest.GetAllAssetBundles();
  61. var count = bundleNames.Length;
  62. var crcManifest = new BundleCrcInfoManifest()
  63. {
  64. bundleInfos = new BundleCrcInfo[count]
  65. };
  66. var noVariant = string.IsNullOrEmpty(Framework.Asset.Define.BUNDLE_VARIANT);
  67. for (var i = 0; i < count; i++)
  68. {
  69. var bundleName = bundleNames[i];
  70. // bundleName不包含后缀,把子文件夹名字也去掉,方便直接拿文件名比对
  71. var sepIndex = bundleName.LastIndexOf('/');
  72. var bundleInfo = new BundleCrcInfo()
  73. {
  74. bundleName = sepIndex >= 0 ? bundleName.Substring(sepIndex + 1) : bundleName,
  75. bundleCRC = manifest.GetAssetBundleCrc(bundleName).ToString()
  76. };
  77. crcManifest.bundleInfos[i] = bundleInfo;
  78. if (noVariant)
  79. {
  80. continue;
  81. }
  82. try
  83. {
  84. var filePath = Path.Combine(outputPath, bundleName);
  85. if (File.Exists(filePath))
  86. {
  87. UnityEditor.FileUtil.MoveFileOrDirectory(filePath, $"{filePath}.{Framework.Asset.Define.BUNDLE_VARIANT}");
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. Debug.LogException(ex);
  93. }
  94. }
  95. var crcManifestPath = Path.Combine(outputPath, "BundleCrcInfoManifest.manifest");
  96. if (File.Exists(crcManifestPath))
  97. {
  98. File.Delete(crcManifestPath);
  99. }
  100. File.WriteAllText(crcManifestPath, crcManifest.ToString());
  101. BuildLog.Log($"PostBuildAssetBundles Completed. Manifest Path:{crcManifestPath}");
  102. }
  103. }
  104. }