【问题标题】:EditorSceneManager using SceneManager in Play mode testEditorSceneManager 在 Play 模式下使用 SceneManager 测试
【发布时间】:2020-12-02 09:33:28
【问题描述】:

我需要使用 EditorSceneManager 加载场景以进行播放模式单元测试。 我需要使用这个而不是 SceneManager.Load,因为我无法在构建设置中拥有场景。关于 EditorSceneManager 的文档很清楚它应该做我需要的。

    protected void LoadScene(string sceneName, Action onComplete)
    {
        if (string.IsNullOrEmpty(sceneName)) { Debug.LogError("Missing scene name"); return;}
        UnityEditor.SceneManagement.EditorSceneManager.LoadScene(sceneName);
        //...
    }

我收到错误消息,场景不在构建设置中,好像调用了 SceneManager.LoadScene 而不是编辑器版本。

编译器还建议简化为我不想使用的以下内容:

 UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);

虽然我没有可能混淆代码的指令:

using System;
using System.Collections;
using UnityEditor;
using UnityEngine;

我使用带有 IPrebuildSetup、IPostBuildCleanup 的辅助解决方案通过 AssetDatabase 和 EditorBuildSettingsScene 添加/删除场景,它可以工作。但它还在构建产品应用程序时在 Jenkins 中运行场景加载/卸载过程。我需要避免这种情况,因为它可能会导致只在测试场景中使用的资产。

【问题讨论】:

    标签: c# unit-testing unity3d editor scene


    【解决方案1】:

    EditorSceneManager 继承自 SceneManager,这就是为什么它提供相同的方法 LoadScene 而 afaik 做同样的事情,这意味着 EditorSceneManager 没有覆盖或类似的东西。

    但是它提供了额外的方法,您可以并且应该将其用于编辑器特定的东西。

    您可以使用LoadSceneInPlayMode

    此方法允许您在播放模式期间在编辑器中加载场景,无需将场景包含在构建设置场景列表中。

    UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(sceneName);
    

    注意:参数被称为path,而不是sceneName,这是有原因的;)

    您必须提供存储在Scene.path中的路径

    返回场景的相对路径。喜欢:“Assets/MyScenes/MyScene.unity”

    【讨论】:

    • 好的,就是这样。我使用了那个及其异步版本无济于事,我只会得到一个具有正确命名的空场景。问题出在场景路径中,它需要是“Assets//sceneName.unity”,而不仅仅是我所做的场景名称。也许您想将其添加到答案中,因为我可以看到这个问题在 Google 上有点泛滥,而且大多数情况下它以“好的,我修复了它”结束并且没有实际的解决方案。
    【解决方案2】:

    你应该写一个

    using UnityEngine.SceneManagement;
    

    然后你可以加载场景

    SceneManager.LoadScene(sceneName);
    

    这样应该没问题

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2021-04-09
    • 1970-01-01
    • 2011-05-25
    • 2023-04-02
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多