BuildPlayerCommand.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using HybridCLR.Editor.Commands;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace HybridCLR.Editor
  8. {
  9. public class BuildPlayerCommand
  10. {
  11. public static void CopyAssetBundles(string outputDir)
  12. {
  13. Directory.CreateDirectory(outputDir);
  14. foreach(var ab in new string[] {"common"})
  15. {
  16. string srcFile = $"{Application.streamingAssetsPath}/{ab}";
  17. string dstFile = $"{outputDir}/{Path.GetFileName(ab)}";
  18. File.Copy(srcFile, dstFile, true);
  19. }
  20. }
  21. //没必要使用
  22. // [MenuItem("HybridCLR/Build/Win64")]
  23. public static void Build_Win64()
  24. {
  25. BuildTarget target = BuildTarget.StandaloneWindows64;
  26. BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
  27. if (activeTarget != BuildTarget.StandaloneWindows64 && activeTarget != BuildTarget.StandaloneWindows)
  28. {
  29. Debug.LogError("请先切到Win平台再打包");
  30. return;
  31. }
  32. // Get filename.
  33. string outputPath = $"{SettingsUtil.ProjectDir}/Release-Win64";
  34. var buildOptions = BuildOptions.CompressWithLz4;
  35. string location = $"{outputPath}/HybridCLRTrial.exe";
  36. PrebuildCommand.GenerateAll();
  37. Debug.Log("====> Build App");
  38. BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
  39. {
  40. scenes = new string[] { "Assets/Scenes/main.unity" },
  41. locationPathName = location,
  42. options = buildOptions,
  43. target = target,
  44. targetGroup = BuildTargetGroup.Standalone,
  45. };
  46. var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
  47. if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
  48. {
  49. Debug.LogError("打包失败");
  50. return;
  51. }
  52. Debug.Log("====> Build AssetBundle");
  53. AssetBundleBuildCommand.BuildAssetBundleByTarget(target);
  54. Debug.Log("====> 复制 AssetBundle");
  55. CopyAssetBundles($"{outputPath}/HybridCLRTrial_Data/StreamingAssets");
  56. #if UNITY_EDITOR
  57. Application.OpenURL($"file:///{location}");
  58. #endif
  59. }
  60. // [MenuItem("HybridCLR/Build/Win32")]
  61. public static void Build_Win32()
  62. {
  63. BuildTarget target = BuildTarget.StandaloneWindows;
  64. BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
  65. if (activeTarget != BuildTarget.StandaloneWindows64 && activeTarget != BuildTarget.StandaloneWindows)
  66. {
  67. Debug.LogError("请先切到Win平台再打包");
  68. return;
  69. }
  70. // Get filename.
  71. string outputPath = $"{SettingsUtil.ProjectDir}/Release-Win32";
  72. PrebuildCommand.GenerateAll();
  73. var buildOptions = BuildOptions.CompressWithLz4;
  74. string location = $"{outputPath}/HybridCLRTrial.exe";
  75. Debug.Log("====> Build App");
  76. BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
  77. {
  78. scenes = new string[] { "Assets/Scenes/main.unity" },
  79. locationPathName = location,
  80. options = buildOptions,
  81. target = target,
  82. targetGroup = BuildTargetGroup.Standalone,
  83. };
  84. var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
  85. if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
  86. {
  87. Debug.LogError("打包失败");
  88. return;
  89. }
  90. Debug.Log("====> Build AssetBundle");
  91. AssetBundleBuildCommand.BuildAssetBundleByTarget(target);
  92. Debug.Log("====> 复制 AssetBundle");
  93. CopyAssetBundles($"{outputPath}/HybridCLRTrial_Data/StreamingAssets");
  94. #if UNITY_EDITOR
  95. Application.OpenURL($"file:///{outputPath}");
  96. #endif
  97. }
  98. // [MenuItem("HybridCLR/Build/Android64")]
  99. public static void Build_Android64()
  100. {
  101. BuildTarget target = BuildTarget.Android;
  102. BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
  103. if (activeTarget != BuildTarget.Android)
  104. {
  105. Debug.LogError("请先切到Android平台再打包");
  106. return;
  107. }
  108. // Get filename.
  109. string outputPath = $"{SettingsUtil.ProjectDir}/Release-Android";
  110. var buildOptions = BuildOptions.CompressWithLz4;
  111. string location = outputPath + "/HybridCLRTrial.apk";
  112. PrebuildCommand.GenerateAll();
  113. BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
  114. {
  115. scenes = new string[] { "Assets/Scenes/main.unity" },
  116. locationPathName = location,
  117. options = buildOptions,
  118. target = target,
  119. targetGroup = BuildTargetGroup.Android,
  120. };
  121. Debug.Log("====> 第1次 Build App(为了生成补充AOT元数据dll)");
  122. BuildPipeline.BuildPlayer(buildPlayerOptions);
  123. Debug.Log("====> Build AssetBundle");
  124. AssetBundleBuildCommand.BuildAssetBundleByTarget(target);
  125. Debug.Log("====> 第2次打包");
  126. BuildPipeline.BuildPlayer(buildPlayerOptions);
  127. #if UNITY_EDITOR
  128. Application.OpenURL($"file:///{outputPath}");
  129. #endif
  130. }
  131. }
  132. }