using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; namespace Unity.Tutorials.Core.Editor { /// /// Needed by BuildStartedCriterion. /// public abstract class PreprocessBuildCriterion : Criterion, IPreprocessBuildWithReport { /// /// See UnityEditor.Build.IPreprocessBuildWithReport.callbackOrder. /// public int callbackOrder => 0; /// /// See UnityEditor.Build.IPreprocessBuildWithReport.OnPreprocessBuild. /// /// public abstract void OnPreprocessBuild(BuildReport report); } /// /// Tests if a build has started. /// // TODO revisit this code, BuildPlayerWindow.RegisterBuildPlayerHandler works only when // building from the default build dialog, hence IPreprocessBuildWithReport + SessionState used also. public class BuildStartedCriterion : PreprocessBuildCriterion { bool BuildStarted { get => SessionState.GetBool("BuildStartedCriterion.BuildStarted", false); set => SessionState.SetBool("BuildStartedCriterion.BuildStarted", value); } /// /// Used for BuildPlayerWindow.RegisterBuildPlayerHandler. /// /// public void BuildPlayerCustomHandler(BuildPlayerOptions options) { BuildStarted = true; BuildPipeline.BuildPlayer(options); } /// /// Starts testing of the criterion. /// public override void StartTesting() { BuildStarted = false; UpdateCompletion(); BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerCustomHandler); EditorApplication.update += UpdateCompletion; } /// /// Stops testing of the criterion. /// public override void StopTesting() { BuildPlayerWindow.RegisterBuildPlayerHandler(null); EditorApplication.update -= UpdateCompletion; } /// /// Evaluates if the criterion is completed. /// /// protected override bool EvaluateCompletion() { return BuildStarted; } /// /// Auto-completes the criterion. /// /// True if the auto-completion succeeded. public override bool AutoComplete() { return true; } /// /// UnityEditor.Build.IPreprocessBuildWithReport override, do not call. /// /// public override void OnPreprocessBuild(BuildReport report) { BuildStarted = true; } } }