BuildHelper.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Sirenix.Utilities;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using YooAsset.Editor;
  8. namespace ET
  9. {
  10. public static class BuildHelper
  11. {
  12. private const string relativeDirPrefix = "../Release";
  13. public static string BuildFolder = "../Release/{0}/StreamingAssets/";
  14. /*[InitializeOnLoadMethod]
  15. public static void ReGenerateProjectFiles()
  16. {
  17. if (Unity.CodeEditor.CodeEditor.CurrentEditor.GetType().Name == "RiderScriptEditor")
  18. {
  19. FieldInfo generator = Unity.CodeEditor.CodeEditor.CurrentEditor.GetType().GetField("m_ProjectGeneration", BindingFlags.Static | BindingFlags.NonPublic);
  20. var syncMethod = generator.FieldType.GetMethod("Sync");
  21. syncMethod.Invoke(generator.GetValue(Unity.CodeEditor.CodeEditor.CurrentEditor), null);
  22. }
  23. else
  24. {
  25. Unity.CodeEditor.CodeEditor.CurrentEditor.SyncAll();
  26. }
  27. Debug.Log("ReGenerateProjectFiles finished.");
  28. }*/
  29. #if ENABLE_CODES
  30. [MenuItem("ET/ChangeDefine/Remove ENABLE_CODES")]
  31. public static void RemoveEnableCodes()
  32. {
  33. EnableCodes(false);
  34. }
  35. #else
  36. [MenuItem("ET/ChangeDefine/Add ENABLE_CODES")]
  37. public static void AddEnableCodes()
  38. {
  39. EnableCodes(true);
  40. }
  41. #endif
  42. private static void EnableCodes(bool enable)
  43. {
  44. string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
  45. var ss = defines.Split(';').ToList();
  46. if (enable)
  47. {
  48. if (ss.Contains("ENABLE_CODES"))
  49. {
  50. return;
  51. }
  52. ss.Add("ENABLE_CODES");
  53. }
  54. else
  55. {
  56. if (!ss.Contains("ENABLE_CODES"))
  57. {
  58. return;
  59. }
  60. ss.Remove("ENABLE_CODES");
  61. }
  62. defines = string.Join(";", ss);
  63. PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
  64. AssetDatabase.SaveAssets();
  65. }
  66. #if ENABLE_VIEW
  67. [MenuItem("ET/ChangeDefine/Remove ENABLE_VIEW")]
  68. public static void RemoveEnableView()
  69. {
  70. EnableView(false);
  71. }
  72. #else
  73. [MenuItem("ET/ChangeDefine/Add ENABLE_VIEW")]
  74. public static void AddEnableView()
  75. {
  76. EnableView(true);
  77. }
  78. #endif
  79. private static void EnableView(bool enable)
  80. {
  81. string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
  82. var ss = defines.Split(';').ToList();
  83. if (enable)
  84. {
  85. if (ss.Contains("ENABLE_VIEW"))
  86. {
  87. return;
  88. }
  89. ss.Add("ENABLE_VIEW");
  90. }
  91. else
  92. {
  93. if (!ss.Contains("ENABLE_VIEW"))
  94. {
  95. return;
  96. }
  97. ss.Remove("ENABLE_VIEW");
  98. }
  99. defines = string.Join(";", ss);
  100. PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
  101. AssetDatabase.SaveAssets();
  102. }
  103. public static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions, BuildOptions buildOptions, bool isBuildExe, bool isContainAB, bool clearFolder)
  104. {
  105. BuildTarget buildTarget = BuildTarget.StandaloneWindows;
  106. string programName = "ET";
  107. string exeName = programName;
  108. switch (type)
  109. {
  110. case PlatformType.Windows:
  111. buildTarget = BuildTarget.StandaloneWindows64;
  112. exeName += ".exe";
  113. break;
  114. case PlatformType.Android:
  115. buildTarget = BuildTarget.Android;
  116. exeName += ".apk";
  117. break;
  118. case PlatformType.IOS:
  119. buildTarget = BuildTarget.iOS;
  120. break;
  121. case PlatformType.MacOS:
  122. buildTarget = BuildTarget.StandaloneOSX;
  123. break;
  124. case PlatformType.Linux:
  125. buildTarget = BuildTarget.StandaloneLinux64;
  126. break;
  127. }
  128. /*string fold = string.Format(BuildFolder, type);
  129. if (clearFolder && Directory.Exists(fold))
  130. {
  131. Directory.Delete(fold, true);
  132. }
  133. Directory.CreateDirectory(fold);
  134. if (isContainAB)
  135. {
  136. Debug.Log("start build assetbundle");
  137. BuildAB(buildTarget, fold);
  138. //BuildPipeline.BuildAssetBundles(fold, buildAssetBundleOptions, buildTarget);
  139. Debug.Log("finish build assetbundle");
  140. FileHelper.CleanDirectory("Assets/StreamingAssets/");
  141. FileHelper.CopyDirectory(fold, "Assets/StreamingAssets/");
  142. AssetDatabase.Refresh();
  143. }*/
  144. if (isBuildExe)
  145. {
  146. Debug.Log("start build exe==================");
  147. BuildPipeline.BuildPlayer(new string[] { "Assets/Scenes/Init.unity" }, $"{relativeDirPrefix}/{exeName}", buildTarget, buildOptions);
  148. Debug.Log("build exe end====================");
  149. }
  150. else
  151. {
  152. /*if (isContainAB && type == PlatformType.Windows)
  153. {
  154. string targetPath = Path.Combine(relativeDirPrefix, $"{programName}_Data/StreamingAssets/");
  155. FileHelper.CleanDirectory(targetPath);
  156. Debug.Log($"src dir: {fold} target: {targetPath}");
  157. FileHelper.CopyDirectory(fold, targetPath);
  158. }*/
  159. }
  160. }
  161. //TODO:默认不使用加密
  162. public static void BuildAB(BuildTarget _buildTarget, string outDir, int buildVersion = -1)
  163. {
  164. string defaultOutputRoot = outDir.IsNullOrWhitespace() ? AssetBundleBuilderHelper.GetDefaultOutputRoot() : outDir;
  165. BuildParameters buildParameters = new BuildParameters();
  166. buildParameters.OutputRoot = defaultOutputRoot;
  167. buildParameters.BuildTarget = _buildTarget;
  168. buildParameters.BuildPipeline = AssetBundleBuilderSettingData.Setting.BuildPipeline;
  169. buildParameters.BuildMode = AssetBundleBuilderSettingData.Setting.BuildMode;
  170. buildParameters.BuildVersion = buildVersion > 0 ? buildVersion : AssetBundleBuilderSettingData.Setting.BuildVersion;
  171. buildParameters.BuildinTags = AssetBundleBuilderSettingData.Setting.BuildTags;
  172. buildParameters.VerifyBuildingResult = true;
  173. buildParameters.EnableAddressable = AssetBundleCollectorSettingData.Setting.EnableAddressable;
  174. buildParameters.CopyBuildinTagFiles = AssetBundleBuilderSettingData.Setting.BuildMode == EBuildMode.ForceRebuild;
  175. buildParameters.EncryptionServices = null;//CreateEncryptionServicesInstance();
  176. buildParameters.CompressOption = AssetBundleBuilderSettingData.Setting.CompressOption;
  177. buildParameters.OutputNameStyle = AssetBundleBuilderSettingData.Setting.OutputNameStyle;
  178. if (AssetBundleBuilderSettingData.Setting.BuildPipeline == EBuildPipeline.ScriptableBuildPipeline)
  179. {
  180. buildParameters.SBPParameters = new BuildParameters.SBPBuildParameters();
  181. buildParameters.SBPParameters.WriteLinkXML = true;
  182. }
  183. var builder = new AssetBundleBuilder();
  184. if (builder.Run(buildParameters).Success)
  185. {
  186. EditorUtility.RevealInFinder($"{buildParameters.OutputRoot}/{buildParameters.BuildTarget}/{buildParameters.BuildVersion}");
  187. }
  188. }
  189. }
  190. }