BuildRunner.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. var taskAttribute = task.GetType().GetCustomAttribute<TaskAttribute>();
  36. if (taskAttribute != null)
  37. BuildLogger.Log($"---------------------------------------->{taskAttribute.TaskDesc}<---------------------------------------");
  38. task.Run(context);
  39. _buildWatch.Stop();
  40. // 统计耗时
  41. int seconds = GetBuildSeconds();
  42. TotalSeconds += seconds;
  43. if (taskAttribute != null)
  44. BuildLogger.Log($"{taskAttribute.TaskDesc}耗时:{seconds}秒");
  45. }
  46. catch (Exception e)
  47. {
  48. EditorTools.ClearProgressBar();
  49. buildResult.FailedTask = task.GetType().Name;
  50. buildResult.ErrorInfo = e.ToString();
  51. buildResult.Success = false;
  52. break;
  53. }
  54. }
  55. // 返回运行结果
  56. BuildLogger.Log($"构建过程总计耗时:{TotalSeconds}秒");
  57. return buildResult;
  58. }
  59. private static int GetBuildSeconds()
  60. {
  61. float seconds = _buildWatch.ElapsedMilliseconds / 1000f;
  62. return (int)seconds;
  63. }
  64. }
  65. }