BuildTools.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. using System;
  7. public class BuildTools : MonoBehaviour
  8. {
  9. private static string GameName = PlayerSettings.productName + "_" + DateTime.Now.ToString("yyyyMMddHHmm");
  10. private static string OutPutPath = System.IO.Directory.GetCurrentDirectory() + "/BuildTools/OutPut/";
  11. //private static string ApkPath = string.Format("{0}{1}.apk", OutPutPath, GameName);
  12. //private static string ApkAndroidPath = string.Format("{0}{1}", OutPutPath, GameName+ "Android");
  13. //private static string EXEPath = string.Format("{0}{1}.exe", OutPutPath + GameName+ "_PC" + "/",GameName);
  14. //private static string AndroidProjectPath = string.Format("{0}{1}_AndroidProject", OutPutPath, GameName);
  15. [MenuItem("Tools/打包/打包APK")]
  16. public static void BuildAPK()
  17. {
  18. string versionName = "1.0.0";
  19. int versionCode = 1;
  20. string productName = "";
  21. string WorkSpace = "";
  22. bool isDevelopment = false;
  23. Debug.Log("------------- 接收命令行参数 -------------");
  24. List<string> commondList = new List<string>();
  25. foreach (string arg in System.Environment.GetCommandLineArgs())
  26. {
  27. Debug.Log("命令行传递过来参数:" + arg);
  28. commondList.Add(arg);
  29. }
  30. try
  31. {
  32. Debug.Log("命令行传递过来参数数量:" + commondList.Count);
  33. versionName = commondList[commondList.Count - 3];
  34. productName = commondList[commondList.Count - 2];
  35. versionCode = int.Parse(commondList[commondList.Count - 1]);
  36. //versionCode = (int)commondList[commondList.Count - 1];
  37. }
  38. catch (Exception e)
  39. {
  40. print(e.Message);
  41. }
  42. string ApkPath = string.Format("{0}{1}.apk", OutPutPath, productName);
  43. Debug.Log("------------- 更新资源 -------------");
  44. //OneKeyRefreshSource.Create();
  45. Debug.Log("------------- 开始 BuildAPK -------------");
  46. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
  47. PlayerSettings.bundleVersion = versionName;
  48. PlayerSettings.Android.bundleVersionCode = versionCode;
  49. PlayerSettings.Android.useCustomKeystore = true;
  50. // keystore 路径
  51. PlayerSettings.Android.keystoreName = "D:\\jenkins\\script\\zeewain.keystore";
  52. // one.keystore 密码
  53. PlayerSettings.Android.keystorePass = "zeewain123";
  54. // one.keystore 别名
  55. PlayerSettings.Android.keyaliasName = "zeewain";
  56. // 别名密码
  57. PlayerSettings.Android.keyaliasPass = "zeewain123";
  58. //打包安卓工程为true
  59. EditorUserBuildSettings.exportAsGoogleAndroidProject = false;
  60. SetSplashScreen();
  61. BuildOptions buildOption = BuildOptions.None;
  62. if (isDevelopment)
  63. buildOption |= BuildOptions.Development;
  64. else
  65. buildOption &= BuildOptions.Development;
  66. //PlayerSettings.Android.keystorePass = "00000000"; // 密钥密码
  67. //PlayerSettings.Android.keyaliasName = "test.keystore"; // 密钥别名
  68. //PlayerSettings.Android.keyaliasPass = "00000000";
  69. BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, ApkPath, BuildTarget.Android, buildOption);
  70. Debug.Log("------------- 结束 BuildAPK -------------");
  71. Debug.Log("Build目录:" + ApkPath);
  72. Debug.Log("BuildEnd");
  73. //Application.OpenURL(OutPutPath);
  74. }
  75. public static void SetSplashScreen()
  76. {
  77. PlayerSettings.SplashScreen.show = true;
  78. PlayerSettings.SplashScreen.showUnityLogo = false;
  79. var logo = new PlayerSettings.SplashScreenLogo();
  80. //设置闪屏时间
  81. logo.duration = 2f;
  82. //设置闪屏背景颜色
  83. PlayerSettings.SplashScreen.backgroundColor = Color.black;
  84. //设置闪屏logo
  85. if (AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Resources/Logo/logo.png") != null)
  86. {
  87. logo.logo = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Resources/Logo/logo.png");
  88. PlayerSettings.SplashScreen.logos = new PlayerSettings.SplashScreenLogo[1] { logo };
  89. }
  90. }
  91. [MenuItem("Tools/打包/打包Android")]
  92. public static void BuildAndroid()
  93. {
  94. string versionName = "1.0.0";
  95. int versionCode = 1;
  96. string productName = "";
  97. string WorkSpace = "";
  98. bool isDevelopment = false;
  99. Debug.Log("------------- 接收命令行参数 -------------");
  100. List<string> commondList = new List<string>();
  101. foreach (string arg in System.Environment.GetCommandLineArgs())
  102. {
  103. Debug.Log("命令行传递过来参数:" + arg);
  104. commondList.Add(arg);
  105. }
  106. try
  107. {
  108. Debug.Log("命令行传递过来参数数量:" + commondList.Count);
  109. versionName = commondList[commondList.Count - 3];
  110. productName = commondList[commondList.Count - 2];
  111. //versionCode = (int)commondList[commondList.Count - 1];
  112. versionCode = int.Parse(commondList[commondList.Count - 1]);
  113. }
  114. catch (Exception e)
  115. {
  116. print(e.Message);
  117. }
  118. string[] m_Array;
  119. string m_ProductName = "";
  120. m_Array = productName.Split('_');
  121. m_ProductName = m_Array[0];
  122. string AndroidPath = string.Format("{0}{1}", OutPutPath, productName + "/" + m_ProductName);
  123. Debug.Log("------------- 更新资源 -------------");
  124. //OneKeyRefreshSource.Create();
  125. Debug.Log("------------- 开始 BuildApkAndroid -------------");
  126. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
  127. PlayerSettings.bundleVersion = versionName;
  128. PlayerSettings.Android.bundleVersionCode = versionCode;
  129. // keystore 路径
  130. PlayerSettings.Android.keystoreName = "D:\\jenkins\\script\\zeewain.keystore";
  131. // one.keystore 密码
  132. PlayerSettings.Android.keystorePass = "zeewain123";
  133. // one.keystore 别名
  134. PlayerSettings.Android.keyaliasName = "zeewain";
  135. // 别名密码
  136. PlayerSettings.Android.keyaliasPass = "zeewain123";
  137. //打包安卓工程为true
  138. EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
  139. SetSplashScreen();
  140. BuildOptions buildOption = BuildOptions.None;
  141. if (isDevelopment)
  142. buildOption |= BuildOptions.Development;
  143. else
  144. buildOption &= BuildOptions.Development;
  145. //PlayerSettings.Android.keystorePass = "00000000"; // 密钥密码
  146. //PlayerSettings.Android.keyaliasName = "test.keystore"; // 密钥别名
  147. //PlayerSettings.Android.keyaliasPass = "00000000";
  148. BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, AndroidPath, BuildTarget.Android, buildOption);
  149. Debug.Log("------------- 结束 BuildAndroid -------------");
  150. Debug.Log("Build目录:" + AndroidPath);
  151. Debug.Log("BuildEnd");
  152. //Application.OpenURL(OutPutPath);
  153. }
  154. [MenuItem("Tools/打包/打包EXE")]
  155. public static void BuildEXE()
  156. {
  157. string versionName = "1.0.0";
  158. int versionCode = 1;
  159. string productName = "";
  160. string WorkSpace = "";
  161. bool isDevelopment = false;
  162. Debug.Log("------------- 接收命令行参数 -------------");
  163. List<string> commondList = new List<string>();
  164. foreach (string arg in System.Environment.GetCommandLineArgs())
  165. {
  166. Debug.Log("命令行传递过来参数:" + arg);
  167. commondList.Add(arg);
  168. }
  169. try
  170. {
  171. Debug.Log("命令行传递过来参数数量:" + commondList.Count);
  172. versionName = commondList[commondList.Count - 3];
  173. productName = commondList[commondList.Count - 2];
  174. versionCode = int.Parse(commondList[commondList.Count - 1]);
  175. //versionCode = (int)commondList[commondList.Count - 1];
  176. }
  177. catch (Exception e)
  178. {
  179. print(e.Message);
  180. }
  181. string EXEPath = string.Format("{0}{1}.exe", OutPutPath + productName + "/", productName);
  182. Debug.Log("------------- 更新资源 -------------");
  183. //OneKeyRefreshSource.Create();
  184. Debug.Log("------------- 开始 BuildEXE -------------");
  185. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows);
  186. PlayerSettings.bundleVersion = versionName;
  187. PlayerSettings.Android.bundleVersionCode = versionCode;
  188. PlayerSettings.resizableWindow = false;
  189. PlayerSettings.fullScreenMode = FullScreenMode.FullScreenWindow;
  190. SetSplashScreen();
  191. BuildOptions buildOption = BuildOptions.None;
  192. if (isDevelopment)
  193. buildOption |= BuildOptions.Development;
  194. else
  195. buildOption &= BuildOptions.Development;
  196. //PlayerSettings.Android.keystorePass = "00000000"; // 密钥密码
  197. //PlayerSettings.Android.keyaliasName = "test.keystore"; // 密钥别名
  198. //PlayerSettings.Android.keyaliasPass = "00000000";
  199. BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, EXEPath, BuildTarget.StandaloneWindows64, buildOption);
  200. Debug.Log("------------- 结束 BuildEXE -------------");
  201. Debug.Log("Build目录:" + EXEPath);
  202. Debug.Log("BuildEnd");
  203. //Application.OpenURL(OutPutPath);
  204. }
  205. }