CmdBuildPlayer.cs 4.8 KB

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