【问题标题】:How do I unload additive scenes?如何卸载附加场景?
【发布时间】:2021-09-15 00:04:27
【问题描述】:

我正在制作一款坦克对战游戏,它会在每一轮后随机生成新关卡。在我的游戏管理器中,我试图从随机范围索引中的加法加载场景开始这一轮,然后通过卸载场景然后加载新的随机场景来结束。但是,我所做的每一次尝试都会导致某种形式的错误。

我一直被引导到 LoadLevelAsync,但它似乎只是给了我更多似乎没有人回答的问题。

目前的布局如下:

//Load random level scene
        int index = Random.Range(2, 4);
        SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
        Debug.Log("SceneLoaded");


//Unload current scene and load new random level scene

        int index = Random.Range(2, 4);
        SceneManager.UnloadSceneAsync(index);
        SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
        Debug.Log("SceneLoaded");

通过设置此代码的方式,如果新的随机级别是先前使用的级别的重复,它似乎可以正常工作,但如果调用的级别不同,那么它会给我一个错误并崩溃。

非常感谢任何关于从这里去哪里的建议。我无论如何都不是程序员,所以需要简单而详细的解释。谢谢。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    //Load random level scene
            int index = Random.Range(2, 4);
            SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
    
    Suppose if here 'index' number is 3 and scene 3 is loaded.ok no problem.
    
    //Unload current scene and load new random level scene
    
            int index = Random.Range(2, 4);
            SceneManager.UnloadSceneAsync(index);
    
    And here 'index' number is generate again such as 2.You want to unlode scene 2 but is not there thats the problem.
    
    Solution :-
    
    //Load random level scene
            int index = Random.Range(2, 4);
            int lodedScene = index;
            SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
            Debug.Log("SceneLoaded");
    
    
    //Unload current scene and load new random level scene
    
            int index = Random.Range(2, 4);
            SceneManager.UnloadSceneAsync(lodedScene);
            SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
            Debug.Log("SceneLoaded");
    

    【讨论】:

    • 我仍然收到一个错误,提示参数异常:要卸载的场景无效。
    【解决方案2】:

    我找到了可行的解决方案。也许它仍然是相关的。

    // Get count of loaded Scenes and last index
    var lastSceneIndex = SceneManager.sceneCount - 1
    
    // Get last Scene by index in all loaded Scenes
    var lastLoadedScene = SceneManager.GetSceneAt(lastSceneIndex)
    
    // Unload Scene
    SceneManager.UnloadSceneAsync(lastLoadedScene);
    

    SceneManager.GetSceneAt() - 获取 SceneManager 加载场景列表中索引处的场景

    SceneManager.sceneCount - 当前加载的场景总数


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2017-06-13
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多