簡體   English   中英

使用SceneManager.LoadScene(int,LoadSceneMode.Additive)/ SceneManager.UnloadSceneAsync(int)加載/卸載級別; 刪除默認級別

[英]Loading/unloading levels using SceneManager.LoadScene(int, LoadSceneMode.Additive) / SceneManager.UnloadSceneAsync(int); Deletes default level

在unity項目中使用疊加加載,加載默認級別(游戲管理器),然后運行一個方法來選擇和激活附加級別(Lighting / Map)。 游戲有輪次,在每輪結束時我運行一個方法來卸載添加劑級別(Lighting / Map),但它也會刪除默認級別。

崩潰時出錯:顯示1無相機渲染

參考了統一文檔頁面;

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 

並使游戲崩潰。

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");
    }

從同一個問題的單獨線程中獲得解決方案的想法。 線程在這里: 如何卸載添加場景?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM