PackPlatformUnityBuild.cs 33 KB

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