我的移动 2D Unity 游戏中有多个场景,我想在启动屏幕中加载所有场景,以便场景传递流畅。我怎样才能做到这一点 ?
如果我这样做,我是否需要更改“Application.LoadScene()”方法,我可以使用什么方法?
我是否需要更改“Application.LoadScene()”方法,以及什么方法
我可以用吗 ?
你需要使用SceneManager.LoadSceneAsync
如果您不希望在加载如此多的场景时阻止 Unity。通过使用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;
and scene.allowSceneActivation = false;
。我有时看到这些会引起问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)