| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  | using System; | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | using System.Linq; | 
					
						
							| 
									
										
										
										
											2021-04-02 09:11:56 +00:00
										 |  |  |  | using System.Reflection; | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  | using UnityBuilderAction.Input; | 
					
						
							|  |  |  |  | using UnityBuilderAction.Reporting; | 
					
						
							|  |  |  |  | using UnityBuilderAction.Versioning; | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | using UnityEditor; | 
					
						
							|  |  |  |  | using UnityEditor.Build.Reporting; | 
					
						
							| 
									
										
										
										
											2021-04-02 09:11:56 +00:00
										 |  |  |  | using UnityEngine; | 
					
						
							| 
									
										
										
										
											2021-03-19 17:50:39 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  | namespace UnityBuilderAction | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |   static class Builder | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  |   { | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |     public static void BuildProject() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |       // Gather values from args | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  |       var options = ArgumentsParser.GetValidatedOptions(); | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       // Gather values from project | 
					
						
							|  |  |  |  |       var scenes = EditorBuildSettings.scenes.Where(scene => scene.enabled).Select(s => s.path).ToArray(); | 
					
						
							| 
									
										
										
										
											2021-01-05 18:19:16 +00:00
										 |  |  |  |        | 
					
						
							|  |  |  |  |       // Get all buildOptions from options | 
					
						
							|  |  |  |  |       BuildOptions buildOptions = BuildOptions.None; | 
					
						
							|  |  |  |  |       foreach (string buildOptionString in Enum.GetNames(typeof(BuildOptions))) { | 
					
						
							|  |  |  |  |         if (options.ContainsKey(buildOptionString)) { | 
					
						
							|  |  |  |  |           BuildOptions buildOptionEnum = (BuildOptions) Enum.Parse(typeof(BuildOptions), buildOptionString); | 
					
						
							|  |  |  |  |           buildOptions |= buildOptionEnum; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       // Define BuildPlayer Options | 
					
						
							| 
									
										
										
										
											2021-01-05 18:19:16 +00:00
										 |  |  |  |       var buildPlayerOptions = new BuildPlayerOptions { | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |         scenes = scenes, | 
					
						
							|  |  |  |  |         locationPathName = options["customBuildPath"], | 
					
						
							|  |  |  |  |         target = (BuildTarget) Enum.Parse(typeof(BuildTarget), options["buildTarget"]), | 
					
						
							| 
									
										
										
										
											2021-01-05 18:19:16 +00:00
										 |  |  |  |         options = buildOptions | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  |       // Set version for this build | 
					
						
							| 
									
										
										
										
											2020-08-16 19:09:34 +00:00
										 |  |  |  |       VersionApplicator.SetVersion(options["buildVersion"]); | 
					
						
							| 
									
										
										
										
											2020-06-24 22:02:05 +00:00
										 |  |  |  |       VersionApplicator.SetAndroidVersionCode(options["androidVersionCode"]); | 
					
						
							| 
									
										
										
										
											2020-07-06 01:41:21 +00:00
										 |  |  |  |        | 
					
						
							|  |  |  |  |       // Apply Android settings | 
					
						
							| 
									
										
										
										
											2021-01-05 18:19:16 +00:00
										 |  |  |  |       if (buildPlayerOptions.target == BuildTarget.Android) | 
					
						
							| 
									
										
										
										
											2020-07-06 01:41:21 +00:00
										 |  |  |  |         AndroidSettings.Apply(options); | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 09:11:56 +00:00
										 |  |  |  |       // Execute default AddressableAsset content build, if the package is installed. | 
					
						
							|  |  |  |  |       // Version defines would be the best solution here, but Unity 2018 doesn't support that, | 
					
						
							|  |  |  |  |       // so we fall back to using reflection instead. | 
					
						
							|  |  |  |  |       var addressableAssetSettingsType = Type.GetType( | 
					
						
							|  |  |  |  |         "UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,Unity.Addressables.Editor"); | 
					
						
							|  |  |  |  |       if (addressableAssetSettingsType != null) | 
					
						
							|  |  |  |  |       { | 
					
						
							|  |  |  |  |         // ReSharper disable once PossibleNullReferenceException, used from try-catch | 
					
						
							|  |  |  |  |         void CallAddressablesMethod(string methodName, object[] args) => addressableAssetSettingsType | 
					
						
							|  |  |  |  |           .GetMethod(methodName, BindingFlags.Static | BindingFlags.Public) | 
					
						
							|  |  |  |  |           .Invoke(null, args); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         try | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |           CallAddressablesMethod("CleanPlayerContent", new object[] { null }); | 
					
						
							|  |  |  |  |           CallAddressablesMethod("BuildPlayerContent", Array.Empty<object>()); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         catch (Exception e) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |           Debug.LogError($"Failed to run default addressables build:\n{e}"); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-03-19 17:50:39 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       // Perform build | 
					
						
							| 
									
										
										
										
											2021-01-05 18:19:16 +00:00
										 |  |  |  |       BuildReport buildReport = BuildPipeline.BuildPlayer(buildPlayerOptions); | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       // Summary | 
					
						
							|  |  |  |  |       BuildSummary summary = buildReport.summary; | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  |       StdOutReporter.ReportSummary(summary); | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 22:43:37 +00:00
										 |  |  |  |       // Result | 
					
						
							|  |  |  |  |       BuildResult result = summary.result; | 
					
						
							| 
									
										
										
										
											2020-04-21 22:08:08 +00:00
										 |  |  |  |       StdOutReporter.ExitWithResult(result); | 
					
						
							| 
									
										
										
										
											2019-11-30 23:23:56 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | } |