BuildAppWindow.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using XGame.Editor.Asset;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Editor.Build
  6. {
  7. public class BuildAppWindow : EditorWindow
  8. {
  9. //[MenuItem("Tools/AssetBundle/一键打包", priority = 2)]
  10. public static void CreateWindow()
  11. {
  12. var window = EditorWindow.GetWindow<BuildAppWindow>(true, "BuildAppWindow");
  13. window.Show();
  14. }
  15. private BuildTarget buildTarget;
  16. private string outputPath;
  17. private string productName;
  18. private string bundleIdentifier;
  19. private string version;
  20. private string versionCode;
  21. private string assetVersion;
  22. private int assetVersionCode;
  23. //private bool isForceRebuildAB = true;
  24. private bool isClearSpriteAtlas;
  25. private GUIContent clearAtlasContent;
  26. /// <summary>
  27. /// 是否导出Android工程
  28. /// </summary>
  29. private bool exportProject = false;
  30. ///// <summary>
  31. ///// 是否分离二进制资源
  32. ///// Unity国际版才能使用
  33. ///// </summary>
  34. //private bool splitApplicationBinary = false;
  35. private ScriptingImplementation scriptImpl;
  36. private AndroidArchitecture androidArchitecture;
  37. private PublishType publishType = PublishType.Release;
  38. private Vector2 scrollPosition;
  39. private void OnEnable()
  40. {
  41. buildTarget = EditorUserBuildSettings.activeBuildTarget;
  42. outputPath = PathDefine.DefaultOutputPath;
  43. if (!Directory.Exists(outputPath))
  44. {
  45. Directory.CreateDirectory(outputPath);
  46. }
  47. version = PlayerSettings.bundleVersion;
  48. versionCode = GetVersionCode(buildTarget);
  49. productName = PlayerSettings.productName;// BuildHelper.GetProductName();
  50. assetVersion = "0.0.1";
  51. assetVersionCode = 1000;
  52. bundleIdentifier = PlayerSettings.applicationIdentifier;// BuildHelper.GetDefaultBundleIdentifier(productName);
  53. scriptImpl = ScriptingImplementation.Mono2x;
  54. androidArchitecture = AndroidArchitecture.ARMv7;
  55. publishType = PublishType.Release;
  56. clearAtlasContent = new GUIContent("打包完删除图集配置(*.SpriteAtlas)", "打包完删除图集配置(*.SpriteAtlas)");
  57. //Debug.Log($"platform:{Application.platform}");
  58. }
  59. //private void OnDisable()
  60. //{
  61. //}
  62. private void OnGUI()
  63. {
  64. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, false, true);
  65. //不可操作区域
  66. EditorGUI.BeginDisabledGroup(true);
  67. buildTarget = (BuildTarget)EditorGUILayout.EnumFlagsField("Build Target", buildTarget);
  68. outputPath = EditorGUILayout.TextField("Output Path", outputPath);
  69. EditorGUI.EndDisabledGroup();
  70. //Browse按钮
  71. EditorGUILayout.BeginHorizontal();
  72. GUILayout.FlexibleSpace();
  73. if (GUILayout.Button("Browse", GUILayout.MaxWidth(75f)))
  74. BrowseForFolder();
  75. EditorGUILayout.EndHorizontal();
  76. //
  77. EditorGUILayout.Space();
  78. productName = EditorGUILayout.TextField("Product Name", productName);
  79. bundleIdentifier = EditorGUILayout.TextField("Bundle Identifier", bundleIdentifier);
  80. version = EditorGUILayout.TextField("Version", version);
  81. if (buildTarget == BuildTarget.Android)
  82. {
  83. int.TryParse(versionCode, out var code);
  84. code = EditorGUILayout.IntField("Version Code", code);
  85. versionCode = code.ToString();
  86. scriptImpl = (ScriptingImplementation)EditorGUILayout.EnumPopup("Scripting Backend", scriptImpl);
  87. if (scriptImpl == ScriptingImplementation.Mono2x)
  88. {
  89. androidArchitecture = AndroidArchitecture.ARMv7;
  90. }
  91. else
  92. {
  93. androidArchitecture = (AndroidArchitecture)EditorGUILayout.EnumPopup("Target Architectures", androidArchitecture);
  94. }
  95. exportProject = EditorGUILayout.Toggle("导出Android工程", exportProject);
  96. //splitApplicationBinary = EditorGUILayout.Toggle("分离obb资源", splitApplicationBinary);
  97. //EditorGUILayout.HelpBox("Unity国际版才能使用obb,否则启动会闪退!!!!", MessageType.Warning);
  98. }
  99. else if (buildTarget == BuildTarget.iOS)
  100. {
  101. versionCode = EditorGUILayout.TextField("Build", versionCode);
  102. }
  103. else
  104. {
  105. scriptImpl = (ScriptingImplementation)EditorGUILayout.EnumPopup("Scripting Backend", scriptImpl);
  106. }
  107. assetVersion = EditorGUILayout.TextField("Assets Version", assetVersion);
  108. assetVersionCode = EditorGUILayout.IntField("Assets Version Code", assetVersionCode);
  109. //
  110. //isForceRebuildAB = EditorGUILayout.Toggle("重新构建AssetBundle", isForceRebuildAB);
  111. isClearSpriteAtlas = EditorGUILayout.Toggle(clearAtlasContent, isClearSpriteAtlas);
  112. publishType = (PublishType)EditorGUILayout.EnumPopup("发布类型", publishType);
  113. var isBuildApp = GUILayout.Button("打包");
  114. EditorGUILayout.EndScrollView();
  115. if (isBuildApp)
  116. {
  117. BuildApp();
  118. Close();
  119. }
  120. }
  121. private string GetVersionCode(BuildTarget target)
  122. {
  123. string versionCode;
  124. switch (target)
  125. {
  126. case BuildTarget.Android:
  127. versionCode = PlayerSettings.Android.bundleVersionCode.ToString();
  128. break;
  129. case BuildTarget.iOS:
  130. versionCode = PlayerSettings.iOS.buildNumber;
  131. break;
  132. //case BuildTarget.StandaloneWindows:
  133. //case BuildTarget.StandaloneWindows64:
  134. // bundleVersion = PlayerSettings.bundleVersion;
  135. // break;
  136. default:
  137. versionCode = "0";
  138. break;
  139. }
  140. return versionCode;
  141. }
  142. private void BrowseForFolder()
  143. {
  144. var newPath = EditorUtility.OpenFolderPanel("Output Folder", outputPath, string.Empty);
  145. if (!string.IsNullOrEmpty(newPath))
  146. {
  147. outputPath = newPath;
  148. }
  149. }
  150. private void BuildApp()
  151. {
  152. var config = new BuildConfig()
  153. {
  154. target = buildTarget,
  155. compatibilityLevel = ApiCompatibilityLevel.NET_Standard_2_0,
  156. scriptImpl = scriptImpl,
  157. isSetScriptDefine = true,
  158. androidCreateSymbols = 0,
  159. //splitApplicationBinary = splitApplicationBinary,
  160. publishType = publishType,
  161. callback = BuildUtils.CallbackToString(BuildCallback),
  162. };
  163. config.project = new BuildConfig.ProjectSettings()
  164. {
  165. productName = productName,
  166. applicationIdentifier = bundleIdentifier,
  167. bundleVersion = version,
  168. versionCode = versionCode,
  169. assetVersionName = assetVersion,
  170. assetVersionCode = assetVersionCode,
  171. isIgnoreGenIlrt = false,//publishType == PublishType.Profiler,
  172. outputPath = outputPath,
  173. //isForceRebuildAB = isForceRebuildAB,
  174. isSaveSpriteAtlas = !isClearSpriteAtlas,
  175. };
  176. config.android = new BuildConfig.AndroidSettings()
  177. {
  178. architecture = androidArchitecture,
  179. exportAsGoogleAndroidProject = exportProject,
  180. };
  181. config.editor = new BuildConfig.EditorSettings()
  182. {
  183. autoExplorer = buildTarget != BuildTarget.iOS,
  184. isNative = true,
  185. };
  186. //var text = Framework.Json.XJson.ToJson(config);
  187. //var configPath = PathDefine.ExtAssetsPath + "/Editor/BuildConfig.json";
  188. //File.WriteAllBytes(configPath, System.Text.Encoding.UTF8.GetBytes(text));
  189. BuildEventHelper.InvokeBeforeBuild();
  190. CommandLineBuild.BuildApp(config);
  191. }
  192. //[MenuItem("Build/Test/TestCallback")]
  193. //public static void TestCallback()
  194. //{
  195. // var callback = BuildUtils.CallbackToString(BuildCallback);
  196. // BuildUtils.BuildCompleted(callback, BuildErrorCode.CmdCompleted);
  197. // var configPath = PathDefine.ExtAssetsPath + "/Editor/BuildConfig.json";
  198. // var dirPath = Path.GetDirectoryName(configPath);
  199. // Debug.Log($"file:{configPath} dir:{dirPath}");
  200. //}
  201. private static void BuildCallback(int code)
  202. {
  203. BuildEventHelper.InvokeAfterBuild(code);
  204. BuildLog.Log($"BuildAppWindow BuildCallback code:{code}");
  205. }
  206. }
  207. }