CmdBuildPlayer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XGame.Framework;
  7. namespace XGame.Editor.Build
  8. {
  9. [BuildCommand((uint)BuildCommandPriority.BuildPlayer)]
  10. class CmdBuildPlayer : BaseBuildCommand, ICommandExecuter
  11. {
  12. public BuildErrorCode Execute()
  13. {
  14. if ((Context.config.operations & BuildOperation.OnlyBuildAssets) != 0)
  15. {
  16. BuildLog.Log("CmdBuildPlayer OnlyBuildAssets.");
  17. return BuildErrorCode.CmdCompleted;
  18. }
  19. var disableScenes = DisableScenesWithoutLaunch();
  20. //校验打包的Scene
  21. string[] levels = GetLevelsFromBuildSettings();
  22. if (levels.Length == 0)
  23. {
  24. BuildLog.Error("No scene to build.");
  25. RevertScenes(disableScenes);
  26. return BuildErrorCode.NoActiveScene;
  27. }
  28. var option = BuildOptions.None;
  29. var type = Context.config.publishType;
  30. switch (type)
  31. {
  32. case PublishType.Debug:
  33. option = BuildOptions.Development;
  34. break;
  35. case PublishType.Release:
  36. option = BuildOptions.None;
  37. break;
  38. case PublishType.Profiler:
  39. option = BuildOptions.Development;
  40. break;
  41. default:
  42. break;
  43. }
  44. //打包App
  45. var report = BuildPipeline.BuildPlayer(levels, Context.appOutputPath, Context.config.target, option);
  46. Enum.TryParse(report.summary.result.ToString(), out BuildErrorCode errorCode);
  47. if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded && report.steps != null)
  48. {
  49. foreach (var step in report.steps)
  50. {
  51. if (step.messages != null)
  52. {
  53. foreach (var msg in step.messages)
  54. {
  55. if (msg.type == LogType.Error || msg.type == LogType.Exception)
  56. {
  57. BuildLog.Log($"Build Report Step:{step.name} LogType:{msg.type} Message:{msg.content}");
  58. }
  59. }
  60. }
  61. }
  62. }
  63. BuildLog.Log($"Build App Finish. Result:{errorCode}");
  64. if (errorCode == BuildErrorCode.Succeeded && Context.config.editor.autoExplorer)
  65. {
  66. try
  67. {
  68. System.Diagnostics.Process.Start("expLorer.exe", Path.GetFullPath(Context.config.project.outputPath));
  69. }
  70. catch (Exception ex)
  71. {
  72. BuildLog.Error($"AutoExplorer failed. Path:{Context.config.project.outputPath}");
  73. BuildLog.Exception(ex);
  74. }
  75. }
  76. RevertScenes(disableScenes);
  77. return errorCode == BuildErrorCode.Succeeded ? BuildErrorCode.CmdCompleted : errorCode;
  78. }
  79. private List<string> DisableScenesWithoutLaunch()
  80. {
  81. var scenes = new List<string>();
  82. var launchScene = GetLaunchScene();
  83. foreach (var scene in EditorBuildSettings.scenes)
  84. {
  85. if (!scene.enabled)
  86. {
  87. continue;
  88. }
  89. var sceneName = Path.GetFileNameWithoutExtension(scene.path);
  90. if (sceneName != launchScene)
  91. {
  92. scene.enabled = false;
  93. scenes.Add(scene.path);
  94. }
  95. }
  96. return scenes;
  97. }
  98. private string GetLaunchScene()
  99. {
  100. var xgameConfig = XGameConfig.Load();
  101. if (xgameConfig != null)
  102. {
  103. return xgameConfig.launchSceneName;
  104. }
  105. return string.Empty;
  106. }
  107. private void RevertScenes(List<string> scenes)
  108. {
  109. foreach (var scene in EditorBuildSettings.scenes)
  110. {
  111. if (scenes.Contains(scene.path))
  112. {
  113. scene.enabled = true;
  114. }
  115. }
  116. }
  117. private string[] GetLevelsFromBuildSettings()
  118. {
  119. List<string> levels = new List<string>();
  120. for (int i = 0; i < EditorBuildSettings.scenes.Length; ++i)
  121. {
  122. if (EditorBuildSettings.scenes[i].enabled)
  123. levels.Add(EditorBuildSettings.scenes[i].path);
  124. }
  125. return levels.ToArray();
  126. }
  127. }
  128. }