在启动画面中加载所有场景
问题描述:
我在我的移动2D统一游戏中有多个场景,我想在启动画面中加载所有场景,以便场景传递顺畅。我怎样才能做到这一点 ?在启动画面中加载所有场景
如果我这样做,是否需要更改“Application.LoadScene()”方法,以及我可以使用什么方法?
答
是否需要更改“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;
。我见过这些导致问题有时。
谢谢你,作品像魅力:) –