[英]Unity scene switching takes too much time
我創建了一個場景中帶有主菜單的游戲,而第二個場景中包含了實際的游戲。 當用戶點擊主菜單場景上的播放按鈕時,它會加載實際的游戲場景,但問題是它花費了太多時間。 我該怎么辦?
這是我正在使用的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame()
{
Debug.Log("Ho gaya....! Mera Ho gya");
Application.Quit();
}
}
您可以使用LoadSceneAsync預先加載級別,但不能激活它,加載完成后會得到回調,然后您可以簡單地在用戶按下應該立即激活的按鈕時激活場景。
除了上述方法外,您還可以等待,直到按下上述按鈕以調用SceneManager.LoadSceneAsync
,但在實際游戲場景加載時顯示了一個窗簾(也稱為加載屏幕)。 協程在這里是您的朋友,因為您可以通過從協程IE yield return
來等待AsyncOperation
的完成
var curtain = Instantiate(CurtainPrefab);
DontDestoryOnLoad(curtain);
yield return SceneManager.LoadSceneAsync(gameSceneIndex);
// Don't allow unload to clean up this object or it'll stop the coroutine before destroying the curtain!
DontDestroyOnLoad(gameObject);
yield return SceneManager.UnloadSceneAsync(mainMenuSceneIndex);
Destroy(curtain);
// Do this last or the Coroutine will stop short
Destroy(gameObject);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.