Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a the build process to fail through a Unity editor script?

Tags:

unity3d

I want to force the build process to fail if some validation conditions are not met.

I've tried using an IPreprocessBuildWithReport with no success:

using UnityEditor.Build;
using UnityEditor.Build.Reporting;

public class BuildProcessor : IPreprocessBuildWithReport
{
    public int callbackOrder => 0;

    public void OnPreprocessBuild(BuildReport report)
    {
        // Attempt 1
        // Does not compile because the 'BuildSummary.result' is read only
        report.summary.result = BuildResult.Failed;

        // Attempt 2
        // Causes a log in the Unity editor, but the build still succeeds
        throw new BuildFailedException("Forced fail");
    }
}

Is there any way to programmatically force the build process to fail?

I'm using Unity 2018.3.8f1.

like image 937
Jorge Galvão Avatar asked Mar 16 '19 16:03

Jorge Galvão


People also ask

How do you stop build and run in Unity?

Very easy to do. Set it as an icon on your desktop and then assign a hotkey directly to the icon = instant build process kill switch.

What is clean build Unity?

Unity Technologies A Clean Build forces a rebuild of the Library folder.

What is Unity editor mode?

On the Unity editor, you have two views, the Game view and the Scene view. The Game view simulates what your final rendered game will look like through your Scene Cameras.


1 Answers

As of 2019.2.14f1, the correct way to stop the build is to throw a BuildFailedException.

Other exception types do not interrupt the build.
Derived exception types do not interrupt the build.
Logging errors most certainly do not interrupt the build.

This is how Unity handles exceptions in PostProcessPlayer:

try
{
    postprocessor.PostProcess(args, out props);
}
catch (System.Exception e)
{
    // Rethrow exceptions during build postprocessing as BuildFailedException, so we don't pretend the build was fine.
    throw new UnityEditor.Build.BuildFailedException(e);
}


Just for clarity, this will NOT stop the build.:

// This is not precisely a BuildFailedException. So the build will go on and succeed.
throw new CustomBuildFailedException();

...

public class CustomBuildFailedException: BuildException() {}

like image 136
marsermd Avatar answered Oct 06 '22 16:10

marsermd