PackPlatformUnityBuild.cs 33 KB

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