2
\$\begingroup\$

I've been working on some custom build scripts, which will manipulate the references of certain MonoBehaviours.

I'm using IPreprocessBuildWithReport to do the manipulation, and I want to reset the references when the build completes. Even if the build fails, I want to do the operation, but I want to do some additional stuff if it fails. I'm also using IPostprocessBuildWithReport and IProcessSceneWithReport as well.

I've tried putting a Debug.Log in OnProcessScene and OnPostProcessBuild. I'm building without JAVA path defined, for Android, or without the Keystore password, which fails the build. The IPreprocessBuildWithReport correctly prints the Debug, but the others don't.

Is there any class or interface which has a callback for Build Failure which I can use? I've tried to get the summary from the BuildReport in OnPreprocessBuild, but the result is always returning Unknown.

Edit: I would also like to make the same changes if the Build was cancelled, rather than just if it was failing.

Platforms: Windows, Android

Unity 2019.3.7f1

\$\endgroup\$
1
\$\begingroup\$

We started to use CompilationPipeline to detect the errors and trigger some custom actions. Here is the script template:

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Compilation;
using UnityEngine;

namespace Fiftytwo.BuildTools
{
    internal class BuildPreprocessor : IPreprocessBuildWithReport
    {
        private static object _compilationContext;
        private static int _compilationErrorCount;


        public int callbackOrder => 0;


        public void OnPreprocessBuild ( BuildReport report )
        {
            _compilationContext = null;
            _compilationErrorCount = 0;

            CompilationPipeline.compilationStarted += CompilationPipelineOnCompilationStarted;
            CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished;
            CompilationPipeline.compilationFinished += CompilationPipelineOnCompilationFinished;
        }

        private static void CompilationPipelineOnCompilationStarted ( object compilationContext )
        {
            _compilationContext = compilationContext;
            _compilationErrorCount = 0;
        }

        private static void CompilationPipelineOnAssemblyCompilationFinished ( string path, CompilerMessage[] messages )
        {
            for( int i = messages?.Length ?? 0; --i >= 0; )
            {
                if( messages[i].type == CompilerMessageType.Error )
                    ++_compilationErrorCount;
            }
        }

        private static void CompilationPipelineOnCompilationFinished ( object compilationContext )
        {
            if( compilationContext != _compilationContext )
                return;

            _compilationContext = null;

            CompilationPipeline.compilationStarted -= CompilationPipelineOnCompilationStarted;
            CompilationPipeline.assemblyCompilationFinished -= CompilationPipelineOnAssemblyCompilationFinished;
            CompilationPipeline.compilationFinished -= CompilationPipelineOnCompilationFinished;

            if( _compilationErrorCount > 0 )
            {
                Debug.LogError( $"Compilation finished with errors ({_compilationErrorCount})" );
                // Custom compilation failure processing
            }
        }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.