unity3d - Unity Editor 检测构建失败的时间

标签 unity3d unity-editor

我正在尝试使用 IPreProcessBuildWithReportIPostProcessBuildWithReport 在构建之前将游戏对象添加到场景中,并在构建完成后立即将其从场景中删除。目标是在开发过程中尽量减少层次结构,但在构建过程中包含这些对象。

我的问题发生在构建失败时(由于某些错误),它在构建开始之前添加了游戏对象,但从未达到在失败时删除游戏对象的代码。

是否有类似的方法来检测构建何时失败而无需构建我自己的构建播放器管道? 如果不需要,我真的不想偏离 unity 的默认构建管道。

如果没有简单的方法,我会考虑是否有一种方法可以跟踪构建失败的异常或错误,并在它发生时运行一个方法。

谢谢,让我知道你们的想法。

编辑:根据要求,这里有一个例子:

    class CustomBuildPipeline : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport
    {

        public int callbackOrder => 0;

        // CALLED BEFORE THE BUILD
        public void OnPreprocessBuild(BuildReport report)
        {
            // Create and add gameobjects to scene
        }


        // CALLED AFTER THE BUILD
        public void OnPostprocessBuild(BuildReport report)
        {
             // Remove gameobjects from scene once built
        }
    }
}

PreProcess Interface PostProcess Interface

我想知道是否可以通过某种方式检测此 buildexception 的时间是否有可能的解决方案被抛出并运行代码。

编辑 2:我找到了这个 post有可能的解决方案,但是,它可能不像我希望的那样优雅。我会尝试一些事情并报告回来。

最佳答案

我使用了 post在'EDIT 2'中提出一个像样的解决方案。我不知道它是否会在 100% 的时间内工作,如果我选择了一个糟糕的解决方案,我希望有人能纠正我。这应该允许我在构建开始之前运行代码,如果构建失败或成功而不更改统一构建管道。

    class CustomBuildPipeline : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport
    {

        public int callbackOrder => 0;

        // CALLED BEFORE THE BUILD
        public void OnPreprocessBuild(BuildReport report)
        {
            // Start listening for errors when build starts
            Application.logMessageReceived += OnBuildError;
        }

        // CALLED DURING BUILD TO CHECK FOR ERRORS
        private void OnBuildError(string condition, string stacktrace, LogType type)
        {
            if (type == LogType.Error)
            {
                // FAILED TO BUILD, STOP LISTENING FOR ERRORS
                Application.logMessageReceived -= OnBuildError;
            }
        }

        // CALLED AFTER THE BUILD
        public void OnPostprocessBuild(BuildReport report)
        {
            // IF BUILD FINISHED AND SUCCEEDED, STOP LOOKING FOR ERRORS
            Application.logMessageReceived -= OnBuildError;
        }
    }
}

关于unity3d - Unity Editor 检测构建失败的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840114/

相关文章:

c# - 如何将unity ios项目集成到原生ios swift项目中?

c# - 网格操作非常慢

c# - 如何调整一个克隆体的形状/尺寸以影响场景 View 中的所有其他克隆体

unity3d - 除了在 Unity3D 中使用 "Assets/Gizmos"之外,如何使用自定义脚本图标

unity3d - 来自 VideoPlayer 的 VideoClp 的音频源的音量控制

c# - C# 上的 WaitForSeconds

unity3d - 使统一音频播放与原始WAV文件完全相同的高频音频以进行听力测试

unity3d - unity 条件字段自定义编辑器

unity3d - 统一: just keep loading infitely on "Application.Reload" every time i enter in play mode

c# - 在 Unity 上以编程方式绑定(bind)脚本属性(编辑模式不是在运行时)