BuildRunner.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Diagnostics;
  6. using UnityEngine;
  7. namespace YooAsset.Editor
  8. {
  9. public class BuildRunner
  10. {
  11. private static Stopwatch _buildWatch;
  12. /// <summary>
  13. /// 总耗时
  14. /// </summary>
  15. public static int TotalSeconds = 0;
  16. /// <summary>
  17. /// 执行构建流程
  18. /// </summary>
  19. /// <returns>如果成功返回TRUE,否则返回FALSE</returns>
  20. public static BuildResult Run(List<IBuildTask> pipeline, BuildContext context)
  21. {
  22. if (pipeline == null)
  23. throw new ArgumentNullException("pipeline");
  24. if (context == null)
  25. throw new ArgumentNullException("context");
  26. BuildResult buildResult = new BuildResult();
  27. buildResult.Success = true;
  28. TotalSeconds = 0;
  29. for (int i = 0; i < pipeline.Count; i++)
  30. {
  31. IBuildTask task = pipeline[i];
  32. try
  33. {
  34. _buildWatch = Stopwatch.StartNew();
  35. string taskName = task.GetType().Name.Split('_')[0];
  36. BuildLogger.Log($"--------------------------------------------->{taskName}<--------------------------------------------");
  37. task.Run(context);
  38. _buildWatch.Stop();
  39. // 统计耗时
  40. int seconds = GetBuildSeconds();
  41. TotalSeconds += seconds;
  42. BuildLogger.Log($"{taskName} It takes {seconds} seconds in total");
  43. }
  44. catch (Exception e)
  45. {
  46. EditorTools.ClearProgressBar();
  47. buildResult.FailedTask = task.GetType().Name;
  48. buildResult.ErrorInfo = e.ToString();
  49. buildResult.Success = false;
  50. break;
  51. }
  52. }
  53. // 返回运行结果
  54. BuildLogger.Log($"Total build process time: {TotalSeconds} seconds");
  55. return buildResult;
  56. }
  57. private static int GetBuildSeconds()
  58. {
  59. float seconds = _buildWatch.ElapsedMilliseconds / 1000f;
  60. return (int)seconds;
  61. }
  62. }
  63. }