PackPlatformUnityBuild.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEditor.Build.Reporting;
  8. using UnityEngine;
  9. using SplashScreenLogo = UnityEditor.PlayerSettings.SplashScreenLogo;
  10. namespace Pack
  11. {
  12. public abstract partial class PackPlatformUnity
  13. {
  14. public override void ChangePlatform()
  15. {
  16. base.ChangePlatform();
  17. try
  18. {
  19. BuildClear();
  20. AssetDatabase.Refresh();
  21. ChangeBuildTarget();
  22. ChangeLuaConfig();
  23. ChangeGameAssets();
  24. ChangeLogin();
  25. ChangeLogo();
  26. CheckAppInfo();
  27. ChangeAppIcon();
  28. ChangeSplashScreen();
  29. ChangePlatformSpecial();
  30. ChangePlayerSettings();
  31. EditorUtility.ClearProgressBar();
  32. AssetDatabase.Refresh();
  33. AssetDatabase.SaveAssets();
  34. }
  35. catch (Exception e)
  36. {
  37. EditorUtility.ClearProgressBar();
  38. PackLog.LogException(e);
  39. throw e;
  40. }
  41. }
  42. public override void BuildAssetBundles()
  43. {
  44. base.BuildAssetBundles();
  45. try
  46. {
  47. BuildClear();
  48. AssetDatabase.Refresh();
  49. ChangeBuildTarget();
  50. ChangeLuaConfig();
  51. ChangeGameAssets();
  52. ChangeLogin();
  53. ChangeLogo();
  54. CheckAppInfo();
  55. ChangeAppIcon();
  56. ChangeSplashScreen();
  57. ChangePlatformSpecial();
  58. ChangePlayerSettings();
  59. EditorUtility.ClearProgressBar();
  60. AssetDatabase.Refresh();
  61. AssetDatabase.SaveAssets();
  62. ChangeAssetsObscure();
  63. AssetBundleMap.BuildAllAssetBundles(resVersionCode);
  64. if (Application.isBatchMode)
  65. {
  66. PackLog.Log(PackConstant.TAG_ResOutPath + BundleBuilderZ.GetAssetbundlesPath());
  67. }
  68. if (Application.isBatchMode)
  69. {
  70. string outPath = GetBuildOutputPath();
  71. PackLog.Log(PackConstant.TAG_AppOutPath + outPath);
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. EditorUtility.ClearProgressBar();
  77. PackLog.LogException(e);
  78. throw e;
  79. }
  80. }
  81. public override void BuildApp()
  82. {
  83. base.BuildApp();
  84. try
  85. {
  86. BuildClear();
  87. AssetDatabase.Refresh();
  88. ChangeBuildTarget();
  89. ChangeLuaConfig();
  90. ChangeGameAssets();
  91. ChangeLogin();
  92. ChangeLogo();
  93. CheckAppInfo();
  94. ChangeAppIcon();
  95. ChangeSplashScreen();
  96. ChangePlatformSpecial();
  97. ChangePlayerSettings();
  98. EditorUtility.ClearProgressBar();
  99. AssetDatabase.Refresh();
  100. AssetDatabase.SaveAssets();
  101. ChangeAssetsObscure();
  102. AssetBundleMap.BuildAllAssetBundles(resVersionCode);
  103. if (Application.isBatchMode)
  104. {
  105. PackLog.Log(PackConstant.TAG_ResOutPath + BundleBuilderZ.GetAssetbundlesPath());
  106. }
  107. BuildTarget buildTarget = GetBuildTarget();
  108. string outPath = GetBuildOutputPath();
  109. string outDirPath = Path.GetDirectoryName(outPath);
  110. if (!Directory.Exists(outDirPath))
  111. {
  112. Directory.CreateDirectory(outDirPath);
  113. }
  114. if (Application.isBatchMode)
  115. {
  116. BuildOptions buildOptions = BuildOptions.None;
  117. buildOptions = ChangeBuildOptionsBefore(BuildOptions.None);
  118. var report = BuildPipeline.BuildPlayer(PackConstant.BuildScenes, outPath, buildTarget, buildOptions);
  119. if (report != null && report.summary.result == BuildResult.Succeeded)
  120. {
  121. BuildAppCompleted(buildOptions);
  122. PackLog.Log(PackConstant.TAG_AppOutPath + outPath);
  123. }
  124. else
  125. {
  126. if (report != null)
  127. throw new Exception("Build Fail !!! report : " + report.summary.result + " errorNum : " + report.summary.totalErrors);
  128. else
  129. throw new Exception("Build Fail !!! report : true");
  130. }
  131. }
  132. else
  133. {
  134. BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
  135. BuildPlayerOptions options = new BuildPlayerOptions();
  136. options.options = BuildOptions.ShowBuiltPlayer;
  137. options.locationPathName = outPath;
  138. EditorUserBuildSettings.SetBuildLocation(buildTarget, outPath);
  139. EditorUserBuildSettings.selectedBuildTargetGroup = buildTargetGroup;
  140. var methodInfo = typeof(BuildPlayerWindow.DefaultBuildMethods).GetMethod("GetBuildPlayerOptionsInternal",
  141. BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
  142. null, new Type[] { typeof(bool), typeof(BuildPlayerOptions) }, null);
  143. options = (BuildPlayerOptions)methodInfo.Invoke(null, new object[] { false, options });
  144. options.target = buildTarget;
  145. options.targetGroup = buildTargetGroup;
  146. options.scenes = PackConstant.BuildScenes;
  147. options.options = ChangeBuildOptionsBefore(options.options);
  148. BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
  149. BuildAppCompleted(options.options);
  150. }
  151. }
  152. catch (Exception e)
  153. {
  154. EditorUtility.ClearProgressBar();
  155. PackLog.LogException(e);
  156. throw e;
  157. }
  158. }
  159. protected void ChangeBuildTarget()
  160. {
  161. EditorUtility.DisplayProgressBar("切换平台", "开始切换平台", 0);
  162. BuildTarget buildTarget = GetBuildTarget();
  163. if (EditorUserBuildSettings.activeBuildTarget == buildTarget) return;
  164. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget);
  165. if (EditorUserBuildSettings.activeBuildTarget != buildTarget)
  166. {
  167. throw new Exception("Change Platform is Fail !!!");
  168. }
  169. EditorUtility.DisplayProgressBar("切换平台", "完成切换平台", 1);
  170. }
  171. protected void ChangeLuaConfig()
  172. {
  173. EditorUtility.DisplayProgressBar("Lua配置", "开始更改Lua配置", 0);
  174. string path = Application.dataPath + PackConstant.LuaPlatformPackPath;
  175. StringBuilder sb = new StringBuilder();
  176. sb.AppendLine("local PlatformPack = {");
  177. sb.AppendFormat("\tURL_KEY = '{0}',", sdkVerifyUrl);
  178. sb.AppendLine();
  179. sb.AppendFormat("\tSERVERLIST_URL = '{0}',", serverListUrl);
  180. sb.AppendLine();
  181. sb.AppendFormat("\tNOTIFY_URL = '{0}',", notifyUrl);
  182. sb.AppendLine();
  183. sb.AppendFormat("\tSPECIAL_INFO_URL = '{0}',", specialInfoUrl);
  184. sb.AppendLine();
  185. sb.AppendFormat("\tCUSTOMER_SERVICE_INFO_URL = '{0}',", customerServiceInfoUrl);
  186. sb.AppendLine();
  187. if (ArrayUtility.Contains(defineSymbols, "HEALTH_BULLETIN"))
  188. {
  189. sb.AppendFormat("\tShowHealthBulletin = true,");
  190. sb.AppendLine();
  191. }
  192. if (specialFuncShield)
  193. {
  194. sb.AppendFormat("\tSpecialFuncShield = true,");
  195. sb.AppendLine();
  196. }
  197. sb.Append("}");
  198. sb.AppendLine();
  199. sb.AppendLine();
  200. sb.Append("return PlatformPack");
  201. string newStr = sb.ToString();
  202. UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
  203. if (File.Exists(path))
  204. {
  205. string destStr = File.ReadAllText(path, uTF8Encoding);
  206. if (newStr == destStr)
  207. return;
  208. }
  209. File.WriteAllText(path, newStr, uTF8Encoding);
  210. AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(path.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  211. EditorUtility.DisplayProgressBar("Lua配置", "完成更改Lua配置", 1);
  212. }
  213. protected void ChangeGameAssets()
  214. {
  215. EditorUtility.DisplayProgressBar("更改游戏资源", "开始更改游戏资源", 0);
  216. string srcPath = Application.dataPath + PackConstant.GameAssetsSourcePath + "/" + gameAssetsPath;
  217. string dstPath = Application.dataPath + PackConstant.GameAssetsDestPath;
  218. string configPath = Application.dataPath + PackConstant.GameAssetsConfigPath;
  219. if (File.Exists(configPath))
  220. {
  221. UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
  222. string jsonStr = File.ReadAllText(configPath, uTF8Encoding);
  223. PackGameAssetsConfig config = JsonUtility.FromJson<PackGameAssetsConfig>(jsonStr);
  224. foreach(var item in config.fullReplacePaths)
  225. {
  226. string path = Path.GetFullPath(dstPath + item);
  227. if (File.Exists(path))
  228. {
  229. File.Delete(path);
  230. }
  231. else if (Directory.Exists(path))
  232. {
  233. DeleteDirectoryAssets(path, true);
  234. }
  235. }
  236. }
  237. List<string> changedPaths = new List<string>();
  238. CopyAndReplaceDirectory(srcPath, dstPath, false, false, changedPaths);
  239. EditorUtility.DisplayProgressBar("更改游戏资源", "开始更改游戏资源", 0.5f);
  240. foreach (var item in changedPaths)
  241. {
  242. string path = Path.GetFullPath(item);
  243. path = FileUtil.GetProjectRelativePath(path.Replace('\\', '/'));
  244. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
  245. }
  246. EditorUtility.DisplayProgressBar("更改游戏资源", "开始更改游戏资源", 1);
  247. }
  248. protected void ChangeLogin()
  249. {
  250. EditorUtility.DisplayProgressBar("更改Login", "开始更改Login", 0);
  251. string directoryPath = PackConstant.LoginSourcePath;
  252. string loginSourcePath = Path.GetFullPath(Application.dataPath + directoryPath + loginRelativePath + PackConstant.Login0SourcePath);
  253. string loginDestPath = Path.GetFullPath(Application.dataPath + PackConstant.Login0DestPath);
  254. if (!File.Exists(loginSourcePath))
  255. {
  256. throw new Exception("File is not Exist " + loginSourcePath);
  257. }
  258. if (!File.Exists(loginDestPath))
  259. {
  260. throw new Exception("File is not Exist " + loginDestPath);
  261. }
  262. File.Copy(loginSourcePath, loginDestPath, true);
  263. AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(loginDestPath.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  264. EditorUtility.DisplayProgressBar("更改Login", "开始更改Login", 0.3f);
  265. loginSourcePath = Path.GetFullPath(Application.dataPath + directoryPath + loginRelativePath + PackConstant.Login1SourcePath);
  266. loginDestPath = Path.GetFullPath(Application.dataPath + PackConstant.Login1DestPath);
  267. if (!File.Exists(loginSourcePath))
  268. {
  269. throw new Exception("File is not Exist " + loginSourcePath);
  270. }
  271. if (!File.Exists(loginDestPath))
  272. {
  273. throw new Exception("File is not Exist " + loginDestPath);
  274. }
  275. File.Copy(loginSourcePath, loginDestPath, true);
  276. AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(loginDestPath.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  277. EditorUtility.DisplayProgressBar("更改Login", "开始更改Login", 0.6f);
  278. loginSourcePath = Path.GetFullPath(Application.dataPath + directoryPath + loginRelativePath + PackConstant.Login2SourcePath);
  279. loginDestPath = Path.GetFullPath(Application.dataPath + PackConstant.Login2DestPath);
  280. if (!File.Exists(loginSourcePath))
  281. {
  282. throw new Exception("File is not Exist " + loginSourcePath);
  283. }
  284. if (!File.Exists(loginDestPath))
  285. {
  286. throw new Exception("File is not Exist " + loginDestPath);
  287. }
  288. File.Copy(loginSourcePath, loginDestPath, true);
  289. AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(loginDestPath.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  290. EditorUtility.DisplayProgressBar("更改Login", "完成开始更改Login", 1);
  291. }
  292. protected void ChangeLogo()
  293. {
  294. EditorUtility.DisplayProgressBar("更改Logo", "开始更改Logo", 0);
  295. string directoryPath = PackConstant.LogoSourcePath;
  296. string logoSourcePath = Path.GetFullPath(Application.dataPath + directoryPath + logo1RelativePath);
  297. string logoDestPath = Path.GetFullPath(Application.dataPath + PackConstant.Logo1DestPath);
  298. if (!File.Exists(logoSourcePath))
  299. {
  300. throw new Exception("File is not Exist " + logoSourcePath);
  301. }
  302. if (!File.Exists(logoDestPath))
  303. {
  304. throw new Exception("File is not Exist " + logoDestPath);
  305. }
  306. File.Copy(logoSourcePath, logoDestPath, true);
  307. AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(logoDestPath.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  308. // logoSourcePath = Path.GetFullPath(Application.dataPath + directoryPath + logo2RelativePath);
  309. // logoDestPath = Path.GetFullPath(Application.dataPath + PackConstant.Logo2DestPath);
  310. // if (!File.Exists(logoSourcePath))
  311. // {
  312. // throw new Exception("File is not Exist " + logoSourcePath);
  313. // }
  314. // if (!File.Exists(logoDestPath))
  315. // {
  316. // throw new Exception("File is not Exist " + logoDestPath);
  317. // }
  318. // File.Copy(logoSourcePath, logoDestPath, true);
  319. // AssetDatabase.ImportAsset(FileUtil.GetProjectRelativePath(logoDestPath.Replace('\\', '/')), ImportAssetOptions.ForceUpdate);
  320. EditorUtility.DisplayProgressBar("更改Logo", "完成开始更改Logo", 1);
  321. }
  322. protected void CheckAppInfo()
  323. {
  324. EditorUtility.DisplayProgressBar("AppInfo设置", "开始更改AppInfo", 0);
  325. string directoryPath = Application.dataPath + PackConstant.AppInfoSourcePath;
  326. if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath))
  327. {
  328. throw new Exception("AppInfo Source Path is Empty " + directoryPath);
  329. }
  330. string srcFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.AppInfoSourcePath + PackConstant.AppInfoMetaPath);
  331. string destFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.AppInfoMetaPath);
  332. if (string.IsNullOrEmpty(srcFileMetaPath) || !File.Exists(srcFileMetaPath))
  333. {
  334. throw new Exception("AppInfo.meta Path is not Exist " + srcFileMetaPath);
  335. }
  336. string destFolder = Path.GetDirectoryName(destFileMetaPath);
  337. if (!Directory.Exists(destFolder))
  338. {
  339. Directory.CreateDirectory(destFolder);
  340. }
  341. File.Copy(srcFileMetaPath, destFileMetaPath, true);
  342. EditorUtility.DisplayProgressBar("AppInfo设置", "完成更改AppInfo", 1);
  343. }
  344. protected void ChangeAppIcon()
  345. {
  346. EditorUtility.DisplayProgressBar("AppIcon设置", "开始更改AppIcon", 0);
  347. string directoryPath = GetAppIconPath();
  348. PackLog.Log(PackConstant.TAG_ICON_PATH + directoryPath);
  349. if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath))
  350. {
  351. throw new Exception("App Icon Source Path is Empty " + directoryPath);
  352. }
  353. string srcFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.AppIconSourcePath + PackConstant.AppIconMetaPath);
  354. string destFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.AppIconMetaPath);
  355. if (string.IsNullOrEmpty(srcFileMetaPath) || !File.Exists(srcFileMetaPath))
  356. {
  357. throw new Exception("AppIcon.meta Path is not Exist " + srcFileMetaPath);
  358. }
  359. string destFolder = Path.GetDirectoryName(destFileMetaPath);
  360. if (!Directory.Exists(destFolder))
  361. {
  362. Directory.CreateDirectory(destFolder);
  363. }
  364. File.Copy(srcFileMetaPath, destFileMetaPath, true);
  365. string destDirectoryPath = Path.GetFullPath(Application.dataPath + PackConstant.AppIconDestPath);
  366. if (!Directory.Exists(destDirectoryPath))
  367. {
  368. Directory.CreateDirectory(destDirectoryPath);
  369. }
  370. string[] files = Directory.GetFiles(directoryPath, "*.*", SearchOption.TopDirectoryOnly);
  371. foreach (var file in files)
  372. {
  373. string fileName = Path.GetFileName(file);
  374. File.Copy(file, Application.dataPath + PackConstant.AppIconDestPath + fileName, true);
  375. }
  376. EditorUtility.DisplayProgressBar("AppIcon设置", "开始更改AppIcon", 0.5f);
  377. directoryPath = FileUtil.GetProjectRelativePath(Path.GetFullPath(Application.dataPath + PackConstant.AppIconDestPath).Replace('\\', '/'));
  378. if (directoryPath.EndsWith("/"))
  379. {
  380. directoryPath = directoryPath.Substring(0, directoryPath.Length - 1);
  381. }
  382. AssetDatabase.ImportAsset(directoryPath, ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate);
  383. directoryPath = directoryPath + "/";
  384. BuildTarget buildTarget = GetBuildTarget();
  385. var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
  386. PlatformIconKind[] platformIconKinds = PlayerSettings.GetSupportedIconKindsForPlatform(buildTargetGroup);
  387. foreach (var platformIconKind in platformIconKinds)
  388. {
  389. PlatformIcon[] platformIcons = PlayerSettings.GetPlatformIcons(buildTargetGroup, platformIconKind);
  390. for (int i = 0, iMax = platformIcons.Length; i < iMax; i++)
  391. {
  392. int size = platformIcons[i].width;
  393. string path = directoryPath + size + PackConstant.AppIconExtersionName;
  394. Texture2D texture2D = AssetDatabase.LoadMainAssetAtPath(path) as Texture2D;
  395. if (texture2D)
  396. {
  397. platformIcons[i].SetTexture(texture2D);
  398. }
  399. else
  400. {
  401. PackLog.LogWarning("Not Found " + size + " Icon At Path " + path + ", Please Set It !!!");
  402. }
  403. }
  404. PlayerSettings.SetPlatformIcons(buildTargetGroup, platformIconKind, platformIcons);
  405. }
  406. if (platformIconKinds == null || platformIconKinds.Length <= 0)
  407. {
  408. int[] sizes = PlayerSettings.GetIconSizesForTargetGroup(buildTargetGroup);
  409. Texture2D[] texture2Ds = new Texture2D[sizes.Length];
  410. for (int i = 0, iMax = sizes.Length; i < iMax; i++)
  411. {
  412. int size = sizes[i];
  413. string path = directoryPath + size + PackConstant.AppIconExtersionName;
  414. Texture2D texture2D = AssetDatabase.LoadMainAssetAtPath(path) as Texture2D;
  415. if (texture2D)
  416. {
  417. texture2Ds[i] = texture2D;
  418. }
  419. else
  420. {
  421. PackLog.LogWarning("Not Found " + size + " Icon At Path " + path + ", Please Set It !!!");
  422. }
  423. }
  424. PlayerSettings.SetIconsForTargetGroup(buildTargetGroup, texture2Ds);
  425. }
  426. EditorUtility.DisplayProgressBar("AppIcon设置", "完成更改AppIcon", 1);
  427. }
  428. protected virtual void ChangeSplashScreen()
  429. {
  430. EditorUtility.DisplayProgressBar("SplashScreen设置", "开始更改SplashScreen", 0);
  431. string directoryPath = Application.dataPath + PackConstant.SplashScreenSourcePath;
  432. if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath))
  433. {
  434. throw new Exception("SplashScreen Source Path is Empty " + directoryPath);
  435. }
  436. string srcFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenSourcePath + PackConstant.SplashScreenMetaPath);
  437. if (string.IsNullOrEmpty(srcFileMetaPath) || !File.Exists(srcFileMetaPath))
  438. {
  439. throw new Exception("SplashScreen.meta Path is not Exist " + srcFileMetaPath);
  440. }
  441. string destFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenMetaPath);
  442. string destFolder = Path.GetDirectoryName(destFileMetaPath);
  443. if (!Directory.Exists(destFolder))
  444. {
  445. Directory.CreateDirectory(destFolder);
  446. }
  447. File.Copy(srcFileMetaPath, destFileMetaPath, true);
  448. EditorUtility.DisplayProgressBar("SplashScreen设置", "更改SplashScreen", 0.1f);
  449. int length = splashScreenRelativeFiles.Count;
  450. for (int i = 0; i < length; i++)
  451. {
  452. string srcFilePath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenSourcePath + splashScreenRelativeFiles[i]);
  453. if (!File.Exists(srcFilePath))
  454. {
  455. throw new Exception("SplashScreen File Path is Empty " + srcFilePath);
  456. }
  457. string destFilePath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenDestPath + splashScreenRelativeFiles[i]);
  458. destFolder = Path.GetDirectoryName(destFilePath);
  459. if (!Directory.Exists(destFolder))
  460. {
  461. Directory.CreateDirectory(destFolder);
  462. }
  463. File.Copy(srcFilePath, destFilePath, true);
  464. srcFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenSourcePath + splashScreenRelativeFiles[i] + ".meta");
  465. if (!File.Exists(srcFilePath))
  466. {
  467. throw new Exception("SplashScreen File Path is Empty " + srcFilePath);
  468. }
  469. destFileMetaPath = Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenDestPath + splashScreenRelativeFiles[i] + ".meta");
  470. destFolder = Path.GetDirectoryName(destFilePath);
  471. if (!Directory.Exists(destFolder))
  472. {
  473. Directory.CreateDirectory(destFolder);
  474. }
  475. File.Copy(srcFileMetaPath, destFileMetaPath, true);
  476. EditorUtility.DisplayProgressBar("SplashScreen设置", "更改SplashScreen", 0.1f + ((i * 0.7f) / length));
  477. }
  478. directoryPath = FileUtil.GetProjectRelativePath(Path.GetFullPath(Application.dataPath + PackConstant.SplashScreenDestPath).Replace('\\', '/'));
  479. if (directoryPath.EndsWith("/"))
  480. {
  481. directoryPath = directoryPath.Substring(0, directoryPath.Length - 1);
  482. }
  483. AssetDatabase.ImportAsset(directoryPath, ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate);
  484. directoryPath = directoryPath + "/";
  485. SplashScreenLogo[] logos = new SplashScreenLogo[length];
  486. for (int i = 0; i < length; i++)
  487. {
  488. string path = directoryPath + splashScreenRelativeFiles[i];
  489. Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
  490. logos[i] = SplashScreenLogo.Create(2f, sprite);
  491. EditorUtility.DisplayProgressBar("SplashScreen设置", "更改SplashScreen", 0.8f + ((i * 0.2f) / length));
  492. }
  493. PlayerSettings.SplashScreen.logos = logos;
  494. PlayerSettings.SplashScreen.show = (length > 0);
  495. EditorUtility.DisplayProgressBar("SplashScreen设置", "完成更改SplashScreen", 1);
  496. }
  497. protected virtual void ChangePlayerSettings()
  498. {
  499. PlayerSettings.productName = appName;
  500. BuildTarget buildTarget = GetBuildTarget();
  501. var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
  502. PlayerSettings.SetApplicationIdentifier(buildTargetGroup, bundleId);
  503. string defineSymbolStr = string.Join(";", defineSymbols);
  504. PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defineSymbolStr);
  505. }
  506. protected virtual void ChangeAssetsObscure()
  507. {
  508. var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
  509. var fileInfo = typeof(AssetsObscureUtil).GetField("s_ObscureKey", bindingFlags);
  510. fileInfo.SetValue(null, obscureKey);
  511. fileInfo = typeof(AssetsObscureUtil).GetField("s_ObscureOffsetMin", bindingFlags);
  512. fileInfo.SetValue(null, obscureOffsetMin);
  513. fileInfo = typeof(AssetsObscureUtil).GetField("s_ObscureOffsetValues", bindingFlags);
  514. fileInfo.SetValue(null, obscureOffsetValues);
  515. }
  516. protected virtual void ChangePlatformSpecial()
  517. {
  518. }
  519. protected virtual BuildOptions ChangeBuildOptionsBefore(BuildOptions buildOptions)
  520. {
  521. return buildOptions;
  522. }
  523. protected virtual void BuildAppCompleted(BuildOptions buildOptions)
  524. {
  525. }
  526. public override void BuildClear()
  527. {
  528. BuildTarget buildTarget = GetBuildTarget();
  529. var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
  530. string defineSymbolStr = string.Join(";", PackConstant.NeedFulDefineSymbols);
  531. PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defineSymbolStr);
  532. DeleteDirectoryAssets(Path.GetFullPath(Application.dataPath + PackConstant.AppInfoDestPath), true);
  533. }
  534. protected static void DeleteDirectoryAssetFile(string file)
  535. {
  536. if (!File.Exists(file)) return;
  537. File.Delete(file);
  538. string metaFilePath = file + ".meta";
  539. if (File.Exists(metaFilePath))
  540. {
  541. File.Delete(metaFilePath);
  542. }
  543. string folder = Path.GetDirectoryName(file);
  544. DeleteDirectoryAssets(folder);
  545. }
  546. protected static void DeleteDirectoryAssets(string dir, bool forceDelete = false)
  547. {
  548. if (!Directory.Exists(dir)) return;
  549. var ls = Directory.GetFileSystemEntries(dir);
  550. if (forceDelete || ls == null || ls.Length <= 0)
  551. {
  552. Directory.Delete(dir, true);
  553. if (dir.EndsWith("/") || dir.EndsWith("\\"))
  554. {
  555. dir = dir.Substring(0, dir.Length - 1);
  556. }
  557. string metaFilePath = dir + ".meta";
  558. if (File.Exists(metaFilePath))
  559. {
  560. File.Delete(metaFilePath);
  561. }
  562. string folder = Path.GetDirectoryName(dir);
  563. DeleteDirectoryAssets(folder);
  564. }
  565. }
  566. protected static void CopyDirectoryAssets(string srcDirPath, string destDirPath)
  567. {
  568. string[] sourceFiles = Directory.GetFiles(srcDirPath, "*.*", SearchOption.AllDirectories);
  569. foreach (var filePath in sourceFiles)
  570. {
  571. string fileName = filePath.Replace('\\', '/');
  572. string destFileName = filePath.Replace(srcDirPath, destDirPath);
  573. string destFolder = Path.GetDirectoryName(destFileName);
  574. if (!Directory.Exists(destFolder))
  575. {
  576. Directory.CreateDirectory(destFolder);
  577. }
  578. File.Copy(fileName, destFileName, true);
  579. }
  580. }
  581. protected static void CopyUnityAssets(string srcPath, List<string> srcDirs, string destPath, string srcMetaPath, string destMetaPath)
  582. {
  583. string srcDirPath = Path.GetFullPath(srcPath);
  584. if (string.IsNullOrEmpty(srcDirPath) || !Directory.Exists(srcDirPath))
  585. {
  586. throw new Exception("App Icon Dest Path is Empty " + srcDirPath);
  587. }
  588. string srcFileMetaPath = Path.GetFullPath(srcMetaPath);
  589. string destFileMetaPath = Path.GetFullPath(destMetaPath);
  590. if (string.IsNullOrEmpty(srcFileMetaPath) || !File.Exists(srcFileMetaPath))
  591. {
  592. throw new Exception("meta Path is not Exist " + srcFileMetaPath);
  593. }
  594. string destFolder = Path.GetDirectoryName(destFileMetaPath);
  595. if (!Directory.Exists(destFolder))
  596. {
  597. Directory.CreateDirectory(destFolder);
  598. }
  599. File.Copy(srcFileMetaPath, destFileMetaPath, true);
  600. string destDirPath = Path.GetFullPath(destPath);
  601. if (!Directory.Exists(destDirPath))
  602. {
  603. Directory.CreateDirectory(destDirPath);
  604. }
  605. for (int i = 0, iMax = srcDirs.Count; i < iMax; i++)
  606. {
  607. CopyDirectoryAssets(srcDirPath + srcDirs[i], destDirPath);
  608. }
  609. destDirPath = FileUtil.GetProjectRelativePath(destDirPath.Replace('\\', '/'));
  610. if (destDirPath.EndsWith("/"))
  611. {
  612. destDirPath = destDirPath.Substring(0, destDirPath.Length - 1);
  613. }
  614. AssetDatabase.ImportAsset(destDirPath, ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate);
  615. }
  616. protected static void CopyAndReplaceDirectory(string srcPath, string dstPath, bool ignoreMetaFiles = true, bool delDstInExist = true, List<string> paths = null)
  617. {
  618. if (delDstInExist)
  619. {
  620. if (Directory.Exists(dstPath))
  621. {
  622. Directory.Delete(dstPath, true);
  623. }
  624. if (File.Exists(dstPath))
  625. {
  626. File.Delete(dstPath);
  627. }
  628. }
  629. if (!Directory.Exists(dstPath))
  630. Directory.CreateDirectory(dstPath);
  631. foreach (var file in Directory.GetFiles(srcPath))
  632. {
  633. bool isMeta = (Path.GetExtension(file) == ".meta");
  634. if (ignoreMetaFiles)
  635. {
  636. if (isMeta) continue;
  637. }
  638. string path = Path.Combine(dstPath, Path.GetFileName(file));
  639. File.Copy(file, path, true);
  640. if (!isMeta && paths != null)
  641. {
  642. paths.Add(path);
  643. }
  644. }
  645. foreach (var dir in Directory.GetDirectories(srcPath))
  646. {
  647. string path = Path.Combine(dstPath, Path.GetFileName(dir));
  648. if (paths != null)
  649. {
  650. paths.Add(path);
  651. }
  652. CopyAndReplaceDirectory(dir, path, ignoreMetaFiles, delDstInExist, paths);
  653. }
  654. }
  655. }
  656. }