ProjectBuildService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. #pragma warning disable
  7. class ProjectBuildService : Editor
  8. {
  9. #region 参数解析
  10. public static string ChannelName
  11. {
  12. get
  13. {
  14. #if UNITY_ANDROID
  15. //这里遍历所有参数,找到 ChannelName 开头的参数, 然后把-符号 后面的字符串返回,
  16. foreach (string arg in Environment.GetCommandLineArgs())
  17. {
  18. if (arg.StartsWith("ChannelName"))
  19. {
  20. return arg.Split("-"[0])[1];
  21. }
  22. }
  23. return "Android";
  24. #elif UNITY_IOS
  25. return "IOS";
  26. #else
  27. return "General";
  28. #endif
  29. }
  30. }
  31. public static string ExportPath
  32. {
  33. get
  34. {
  35. string path = Application.dataPath;
  36. //这里遍历所有参数,找到 ExportPath 开头的参数, 然后把-符号 后面的字符串返回,
  37. foreach (string arg in Environment.GetCommandLineArgs())
  38. {
  39. if (arg.StartsWith("ExportPath"))
  40. {
  41. path = arg.Split("-"[0])[1];
  42. }
  43. }
  44. #if UNITY_WEBGL
  45. return path + "/" + ApplicationMode;
  46. #elif UNITY_IOS
  47. return path += "/" + ChannelName + "/" + ApplicationMode;
  48. #else
  49. return path += "/" + ChannelName + "/" + ApplicationMode + "/";
  50. #endif
  51. }
  52. }
  53. public static AppMode ApplicationMode
  54. {
  55. get
  56. {
  57. //这里遍历所有参数,找到 AppMpde 开头的参数, 然后把-符号 后面的字符串返回,
  58. foreach (string arg in Environment.GetCommandLineArgs())
  59. {
  60. if (arg.StartsWith("AppMode"))
  61. {
  62. return (AppMode)Enum.Parse(typeof(AppMode), arg.Split("-"[0])[1]);
  63. }
  64. }
  65. return AppMode.Developing;
  66. }
  67. }
  68. public static bool IsUseAssetsBundle
  69. {
  70. get
  71. {
  72. //这里遍历所有参数,找到 UseAssetsBundle 开头的参数, 然后把-符号 后面的字符串返回,
  73. foreach (string arg in Environment.GetCommandLineArgs())
  74. {
  75. if (arg.StartsWith("UseAssetsBundle"))
  76. {
  77. return bool.Parse(arg.Split("-"[0])[1]);
  78. }
  79. }
  80. return false;
  81. }
  82. }
  83. public static bool IsUseLua
  84. {
  85. get
  86. {
  87. //这里遍历所有参数,找到 UseLua 开头的参数, 然后把-符号 后面的字符串返回,
  88. foreach (string arg in Environment.GetCommandLineArgs())
  89. {
  90. if (arg.StartsWith("UseLua"))
  91. {
  92. return bool.Parse(arg.Split("-"[0])[1]);
  93. }
  94. }
  95. return false;
  96. }
  97. }
  98. public static string Version
  99. {
  100. get
  101. {
  102. return Application.version + "." + VersionService.LargeVersion + "." + VersionService.SmallVersion;
  103. }
  104. }
  105. #endregion
  106. #region 打包函数
  107. #region 通用
  108. static void PrintDebug()
  109. {
  110. string debugString = "";
  111. debugString += ">>>============================================================自动打包输出============================================================<<<\n";
  112. foreach (string arg in Environment.GetCommandLineArgs())
  113. {
  114. debugString += "参数:" + arg + "\n";
  115. }
  116. debugString += "\n";
  117. debugString += "是否使用 Bundle 打包: " + IsUseAssetsBundle + "\n";
  118. debugString += "是否使用 Lua : " + IsUseLua + "\n";
  119. debugString += "渠道名: " + ChannelName + "\n";
  120. debugString += "发布模式: " + ApplicationMode + "\n";
  121. debugString += "导出路径: " + ExportPath + "\n";
  122. debugString += ">>>====================================================================================================================================<<<\n";
  123. Debug.Log(debugString);
  124. }
  125. static void SetApplicationMode(AppMode mode)
  126. {
  127. string appModeDefine = "";
  128. switch (mode)
  129. {
  130. case AppMode.Developing:
  131. appModeDefine = "APPMODE_DEV"; break;
  132. case AppMode.QA:
  133. appModeDefine = "APPMODE_QA"; break;
  134. case AppMode.Release:
  135. appModeDefine = "APPMODE_REL"; break;
  136. }
  137. AddScriptDefine(appModeDefine);
  138. }
  139. static void SetLua(bool useLua)
  140. {
  141. if (useLua)
  142. {
  143. AddScriptDefine("USE_LUA");
  144. }
  145. }
  146. /// <summary>
  147. /// 切换渠道
  148. /// </summary>
  149. static void ChangeChannel(string channelName)
  150. {
  151. #if UNITY_ANDROID
  152. SchemeDataService.ChangeScheme(channelName);
  153. #endif
  154. }
  155. /// <summary>
  156. ///打包或者使用Bundle流程
  157. /// </summary>
  158. static void UseResourcesOrBundle(bool useBundle)
  159. {
  160. if (useBundle)
  161. {
  162. AddScriptDefine("USE_BUNDLE");
  163. BundlePackage();
  164. #if UNITY_IOS
  165. //删除_Res和_Doc
  166. FileTool.SafeDeleteDirectory(Application.dataPath + "/_Res");
  167. FileTool.SafeDeleteDirectory(Application.dataPath + "/_Doc");
  168. #endif
  169. }
  170. else
  171. {
  172. if (Directory.Exists(Application.dataPath + "/StreamingAssets"))
  173. {
  174. //不使用 Bundle 则删除 StreamingAssets 文件夹
  175. FileTool.SafeDeleteDirectory(Application.dataPath + "/StreamingAssets");
  176. }
  177. }
  178. }
  179. static void BundlePackage()
  180. {
  181. //自动增加小版本号
  182. VersionService.SmallVersion++;
  183. VersionService.CreateVersionFile();
  184. //打Bundle包
  185. PackageService.Package(PackageEditorConfigService.RelyPackages, PackageEditorConfigService.Bundles);
  186. //删除 Resources 文件夹
  187. FileTool.SafeDeleteDirectory(Application.dataPath + "/Resources");
  188. }
  189. #endregion
  190. #region Android
  191. static void BuildForAndroid()
  192. {
  193. SwitchPlatform(BuildTarget.Android);
  194. //输出日志
  195. PrintDebug();
  196. //使用Lua
  197. SetLua(IsUseLua);
  198. //发布模式
  199. SetApplicationMode(ApplicationMode);
  200. //使用Resource或者使用Bundle
  201. UseResourcesOrBundle(IsUseAssetsBundle);
  202. //切换渠道
  203. ChangeChannel(ChannelName);
  204. //设置编译指令
  205. ApplyScriptDefine();
  206. //设置签名
  207. //签名路径
  208. PlayerSettings.Android.keystoreName = "";
  209. //密钥密码
  210. PlayerSettings.Android.keystorePass = "";
  211. //密钥别名
  212. PlayerSettings.Android.keyaliasName = "";
  213. //别名密码
  214. PlayerSettings.Android.keyaliasPass = "";
  215. //打包
  216. string path = ExportPath + "/" + GetPackageName() + ".apk";
  217. #if UNITY_2017_1_OR_NEWER
  218. BuildPlayerOptions bo = new BuildPlayerOptions();
  219. bo.scenes = GetBuildScenes();
  220. bo.target = BuildTarget.Android;
  221. bo.options = BuildOptions.None;
  222. bo.locationPathName = path;
  223. BuildPipeline.BuildPlayer(bo);
  224. #else
  225. BuildOptions option = BuildOptions.None;
  226. if (ApplicationMode == AppMode.Release)
  227. {
  228. option = BuildOptions.Il2CPP;
  229. }
  230. BuildPipeline.BuildPlayer(GetBuildScenes(), path, BuildTarget.Android, option);
  231. #endif
  232. }
  233. #endregion
  234. #region IOS
  235. static void BuildForIOS()
  236. {
  237. SwitchPlatform(BuildTarget.iOS);
  238. //输出日志
  239. PrintDebug();
  240. //使用Lua
  241. SetLua(IsUseLua);
  242. //发布模式
  243. SetApplicationMode(ApplicationMode);
  244. //使用Resource或者使用Bundle
  245. UseResourcesOrBundle(IsUseAssetsBundle);
  246. //切换渠道
  247. ChangeChannel(ChannelName);
  248. //设置编译指令
  249. ApplyScriptDefine();
  250. //打包
  251. #if UNITY_2017_1_OR_NEWER
  252. BuildPlayerOptions bo = new BuildPlayerOptions();
  253. bo.scenes = GetBuildScenes();
  254. bo.target = BuildTarget.iOS;
  255. bo.options = BuildOptions.None;
  256. bo.locationPathName = ExportPath;
  257. BuildPipeline.BuildPlayer(bo);
  258. #else
  259. BuildOptions option = BuildOptions.None;
  260. if (ApplicationMode == AppMode.Release)
  261. {
  262. option = BuildOptions.Il2CPP;
  263. }
  264. BuildPipeline.BuildPlayer(GetBuildScenes(), ExportPath, BuildTarget.iOS, option);
  265. #endif
  266. }
  267. #endregion
  268. #region WEBGL
  269. static void BuildForWEBGL()
  270. {
  271. SwitchPlatform(BuildTarget.WebGL);
  272. //输出日志
  273. PrintDebug();
  274. //使用Lua
  275. SetLua(IsUseLua);
  276. //发布模式
  277. SetApplicationMode(ApplicationMode);
  278. //使用Resource或者使用Bundle
  279. UseResourcesOrBundle(IsUseAssetsBundle);
  280. //切换渠道
  281. ChangeChannel(ChannelName);
  282. //设置编译指令
  283. ApplyScriptDefine();
  284. //打包
  285. BuildOptions option = BuildOptions.None;
  286. if (ApplicationMode != AppMode.Release)
  287. {
  288. option = BuildOptions.Development;
  289. }
  290. BuildPipeline.BuildPlayer(GetBuildScenes(), ExportPath, BuildTarget.WebGL, option);
  291. }
  292. #endregion
  293. #endregion
  294. #region 功能函数
  295. //在这里找出你当前工程所有的场景文件,假设你只想把部分的scene文件打包 那么这里可以写你的条件判断 总之返回一个字符串数组。
  296. static string[] GetBuildScenes()
  297. {
  298. List<string> names = new List<string>();
  299. foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
  300. {
  301. if (e == null)
  302. continue;
  303. if (e.enabled)
  304. names.Add(e.path);
  305. }
  306. return names.ToArray();
  307. }
  308. static string GetPackageName()
  309. {
  310. #if UNITY_WEBGL
  311. return Application.productName;
  312. #else
  313. return Application.productName + "_" + Version + "_"+ ChannelName + "_" + GetModeName(ApplicationMode) +"_"+ GetTimeString();
  314. #endif
  315. }
  316. static string GetTimeString()
  317. {
  318. DateTime date = DateTime.Now;
  319. return date.Year + string.Format("{0:d2}", date.Month) + string.Format("{0:d2}", date.Day) + "_" + string.Format("{0:d2}", date.Hour) + string.Format("{0:d2}", date.Minute);
  320. }
  321. static string GetModeName(AppMode mode)
  322. {
  323. switch (mode)
  324. {
  325. case AppMode.Developing:
  326. return "Dev"; ;
  327. case AppMode.QA:
  328. return "QA"; ;
  329. case AppMode.Release:
  330. return "Rel"; ;
  331. default: return "unknow";
  332. }
  333. }
  334. public static void SetScriptDefine(string symbols)
  335. {
  336. BuildTargetGroup targetGroup = BuildTargetGroup.Unknown;
  337. #if UNITY_ANDROID
  338. targetGroup = BuildTargetGroup.Android;
  339. #elif UNITY_IOS
  340. targetGroup = BuildTargetGroup.iOS;
  341. #endif
  342. string define = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
  343. if (!define.Contains(symbols))
  344. {
  345. define += ";" + symbols;
  346. }
  347. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, define);
  348. }
  349. static List<string> s_defines = new List<string>();
  350. static void AddScriptDefine(string symbols)
  351. {
  352. if(!s_defines.Contains(symbols))
  353. {
  354. s_defines.Add(symbols);
  355. }
  356. }
  357. static void ApplyScriptDefine()
  358. {
  359. BuildTargetGroup targetGroup = BuildTargetGroup.Unknown;
  360. #if UNITY_ANDROID
  361. targetGroup = BuildTargetGroup.Android;
  362. #elif UNITY_IOS
  363. targetGroup = BuildTargetGroup.iOS;
  364. #elif UNITY_WEBGL
  365. targetGroup = BuildTargetGroup.WebGL;
  366. #endif
  367. string define = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
  368. for (int i = 0; i < s_defines.Count; i++)
  369. {
  370. if(!define.Contains(s_defines[i]))
  371. {
  372. define += ";" + s_defines[i];
  373. }
  374. }
  375. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, define);
  376. }
  377. /// <summary>
  378. /// 切换平台
  379. /// </summary>
  380. /// <param name="target"></param>
  381. public static void SwitchPlatform(BuildTarget target)
  382. {
  383. if (EditorUserBuildSettings.activeBuildTarget != target)
  384. {
  385. EditorUserBuildSettings.SwitchActiveBuildTarget(target);
  386. }
  387. }
  388. #endregion
  389. }