首页 > 解决方案 > 使用 SceneManager.LoadScene(int, LoadSceneMode.Additive) / SceneManager.UnloadSceneAsync(int) 加载/卸载关卡;删除默认级别

问题描述

在统一项目中使用附加加载,加载默认级别(游戏管理器),然后运行一个方法来选择和激活附加级别(照明/贴图)。游戏有回合,在每一回合结束时,我都会运行一个方法来卸载附加关卡(光照/贴图),但它也会删除默认关卡。

崩溃时出错:Display 1 No Cameras 渲染

已参考统一文档页面;

https://docs.unity3d.com/ScriptReference/SceneManagement.UnloadSceneOptions.html

https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.html

Section for loading in level:

        private IEnumerator RoundStarting ()
        {
            //Calling method meant to load in additive levels. Seems to work...
            LoadMapForRound();
            // As soon as the round starts reset the tanks and make sure they can't move.
            ResetAllTanks ();
            DisableTankControl ();

            // Snap the camera's zoom and position to something appropriate for the reset tanks.
            m_CameraControl.SetStartPositionAndSize ();

            // Increment the round number and display text showing the players what round it is.
            m_RoundNumber++;
            m_MessageText.text = "ROUND " + m_RoundNumber;

            // Wait for the specified length of time until yielding control back to the game loop.
            yield return m_StartWait;
        }

        //Method meant to load in additive levels.
        private void LoadMapForRound()
        {
            int LevelIndex = Random.Range(2, 4);
            SceneManager.LoadScene(1, LoadSceneMode.Additive);
            SceneManager.LoadScene(LevelIndex, LoadSceneMode.Additive);
            //Debug.Log("SceneLoaded");
        }

Section for unloading level:

 private IEnumerator RoundEnding ()
        {
            // Stop tanks from moving.
            DisableTankControl();

            // Clear the winner from the previous round.
            m_RoundWinner = null;

            // See if there is a winner now the round is over.
            m_RoundWinner = GetRoundWinner();

            // If there is a winner, increment their score.
            if (m_RoundWinner != null)
                m_RoundWinner.m_Wins++;

            // Now the winner's score has been incremented, see if someone has one the game.
            m_GameWinner = GetGameWinner();

            // Get a message based on the scores and whether or not there is a game winner and display it.
            string message = EndMessage();
            m_MessageText.text = message;

            // Wait for the specified length of time until yielding control back to the game loop.
            yield return m_EndWait;

            //calling ethod meant to unload levels, but ends up unloading default level as well.
            DestroyRoundMap();
        }

        //Method meant to unload levels
        private  void DestroyRoundMap()
        {
            SceneManager.UnloadSceneAsync(LevelIndex);
            SceneManager.UnloadSceneAsync(1);
        }

Unload additive levels (Light/Map) at rounds end, keep default level (0), instead the default level gets unloaded 

并使游戏崩溃。

标签: c#unity3d

解决方案


Included local variable within the LoadMapForRound for the Global Variable to be equal to.

    private void LoadMapForRound()
    {
        int mapChosen = Random.Range(2, 4); Solution Line
        LevelLoaded = mapChosen;
        SceneManager.LoadScene(LevelLoaded, LoadSceneMode.Additive);
        SceneManager.LoadScene(1, LoadSceneMode.Additive);
        //Debug.Log("SceneLoaded");
    }

从同一问题的单独线程中获得解决方案的想法。此处的主题: 如何卸载附加场景?


推荐阅读