TaskPrepare.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. namespace YooAsset.Editor
  7. {
  8. [TaskAttribute("资源构建准备工作")]
  9. public class TaskPrepare : IBuildTask
  10. {
  11. void IBuildTask.Run(BuildContext context)
  12. {
  13. var buildParameters = context.GetContextObject<BuildParametersContext>();
  14. buildParameters.BeginWatch();
  15. var buildMode = buildParameters.Parameters.BuildMode;
  16. // 检测构建平台是否合法
  17. if (buildParameters.Parameters.BuildTarget == BuildTarget.NoTarget)
  18. throw new Exception("请选择目标平台");
  19. // 检测构建版本是否合法
  20. if (buildParameters.Parameters.BuildVersion <= 0)
  21. throw new Exception("请先设置版本号");
  22. // 检测输出目录是否为空
  23. if (string.IsNullOrEmpty(buildParameters.PipelineOutputDirectory))
  24. throw new Exception("输出目录不能为空");
  25. if (buildMode != EBuildMode.SimulateBuild)
  26. {
  27. // 检测当前是否正在构建资源包
  28. if (BuildPipeline.isBuildingPlayer)
  29. throw new Exception("当前正在构建资源包,请结束后再试");
  30. // 检测是否有未保存场景
  31. if (EditorTools.HasDirtyScenes())
  32. throw new Exception("检测到未保存的场景文件");
  33. // 保存改动的资源
  34. AssetDatabase.SaveAssets();
  35. }
  36. // 增量更新时候的必要检测
  37. if (buildMode == EBuildMode.IncrementalBuild)
  38. {
  39. // 检测历史版本是否存在
  40. if (AssetBundleBuilderHelper.HasAnyPackageVersion(buildParameters.Parameters.BuildTarget, buildParameters.Parameters.OutputRoot))
  41. {
  42. // 检测构建版本是否合法
  43. int maxPackageVersion = AssetBundleBuilderHelper.GetMaxPackageVersion(buildParameters.Parameters.BuildTarget, buildParameters.Parameters.OutputRoot);
  44. if (buildParameters.Parameters.BuildVersion <= maxPackageVersion)
  45. throw new Exception("构建版本不能小于历史版本");
  46. // 检测补丁包是否已经存在
  47. string packageDirectory = buildParameters.GetPackageDirectory();
  48. if (Directory.Exists(packageDirectory))
  49. throw new Exception($"补丁包已经存在:{packageDirectory}");
  50. // 检测内置资源分类标签是否一致
  51. var oldPatchManifest = AssetBundleBuilderHelper.GetOldPatchManifest(buildParameters.PipelineOutputDirectory);
  52. if (buildParameters.Parameters.BuildinTags != oldPatchManifest.BuildinTags)
  53. throw new Exception($"增量更新时内置资源标签必须一致:{buildParameters.Parameters.BuildinTags} != {oldPatchManifest.BuildinTags}");
  54. }
  55. }
  56. // 如果是强制重建
  57. if (buildMode == EBuildMode.ForceRebuild)
  58. {
  59. // 删除平台总目录
  60. string platformDirectory = $"{buildParameters.Parameters.OutputRoot}/{buildParameters.Parameters.BuildTarget}";
  61. if (EditorTools.DeleteDirectory(platformDirectory))
  62. {
  63. BuildRunner.Log($"删除平台总目录:{platformDirectory}");
  64. }
  65. }
  66. // 如果输出目录不存在
  67. if (EditorTools.CreateDirectory(buildParameters.PipelineOutputDirectory))
  68. {
  69. BuildRunner.Log($"创建输出目录:{buildParameters.PipelineOutputDirectory}");
  70. }
  71. }
  72. }
  73. }