我在我的移动2D Unity游戏中有多个场景,我想在闪屏界面加载所有场景,这样场景之间的过渡会更加流畅。我该怎么做?
如果我这样做了,我需要改变“Application.LoadScene()”方法吗?我可以使用哪种方法?
我在我的移动2D Unity游戏中有多个场景,我想在闪屏界面加载所有场景,这样场景之间的过渡会更加流畅。我该怎么做?
如果我这样做了,我需要改变“Application.LoadScene()”方法吗?我可以使用哪种方法?
SceneManager.LoadSceneAsync
。通过使用SceneManager.LoadSceneAsync
,你将能够显示加载状态。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;
。有时候我看到这些会引起问题。
ArgumentException: SceneManager.SetActiveScene failed; scene 'EN_FlashCard_Landscape_Portable' is not loaded and therefore cannot be set active
...有什么想法吗? - questionaskerSceneManager.SetActiveScene
上。请确保你传递给SceneManager.SetActiveScene
的场景已经被加载。如果没有加载,你会得到这个错误。最后,如果你确定已经加载了场景,请在调用SceneManager.SetActiveScene
前等待一帧。 - Programmer