2017-08-17 62 views
2

我在我的移動2D統一遊戲中有多個場景,我想在啓動畫面中加載所有場景,以便場景傳遞順暢。我怎樣才能做到這一點 ?在啓動畫面中加載所有場景

如果我這樣做,是否需要更改「Application.LoadScene()」方法,以及我可以使用什麼方法?

回答

5

是否需要更改「Application.LoadScene()」方法,以及可以使用什麼方法 ?

如果你不想在加載這麼多場景時阻止Unity,那麼你需要使用SceneManager.LoadSceneAsync。通過使用SceneManager.LoadSceneAsync,您將能夠顯示加載狀態。

我想加載啓動畫面

我所有的場景創建一個場景,並確保任何其他場景之前,這一幕負荷。從那裏你可以從0循環到你場景的最大索引。您可以使用SceneManager.GetSceneByBuildIndex從索引中檢索Scene,然後從SceneManager.SetActiveScene中激活您剛剛檢索的場景。

List<AsyncOperation> allScenes = new List<AsyncOperation>(); 
const int sceneMax = 5; 
bool doneLoadingScenes = false; 

void Startf() 
{ 
    StartCoroutine(loadAllScene()); 
} 

IEnumerator loadAllScene() 
{ 
    //Loop through all scene index 
    for (int i = 0; i < sceneMax; i++) 
    { 
     AsyncOperation scene = SceneManager.LoadSceneAsync(i, LoadSceneMode.Additive); 
     scene.allowSceneActivation = false; 

     //Add to List so that we don't lose the reference 
     allScenes.Add(scene); 

     //Wait until we are done loading the scene 
     while (scene.progress < 0.9f) 
     { 
      Debug.Log("Loading scene #:" + i + " [][] Progress: " + scene.progress); 
      yield return null; 
     } 

     //Laod the next one in the loop 
    } 

    doneLoadingScenes = true; 
    OnFinishedLoadingAllScene(); 
} 

void enableScene(int index) 
{ 
    //Activate the Scene 
    allScenes[index].allowSceneActivation = true; 
    SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(index)); 
} 

void OnFinishedLoadingAllScene() 
{ 
    Debug.Log("Done Loading All Scenes"); 
} 

您可以通過enableScene(int index)啓用場景。請注意,一次只能加載一個場景,您必須按照加載它們的順序激活它們,最後,不要丟失AsyncOperation的參考。這就是爲什麼我將它們存儲在List中。

如果遇到問題,請嘗試刪除allScenes[index].allowSceneActivation = true;scene.allowSceneActivation = false;。我見過這些導致問題有時。

+1

謝謝你,作品像魅力:) –