问题描述
我的移动2D统一游戏中有多个场景,我想将所有场景加载到启动画面中,以使场景传递更加流畅.我怎样才能做到这一点 ?
I have a multiple scenes in my mobile 2D unity game, I want to load all my scenes in splash screen, so that the scene passing would be smooth. How can I do this ?
如果执行此操作,是否需要更改"Application.LoadScene()"方法,可以使用哪种方法?
If I do this, do I need to change "Application.LoadScene()" method, and what method can I use ?
推荐答案
如果您不希望在加载这么多场景时阻止Unity的使用,请使用SceneManager.LoadSceneAsync
.通过使用SceneManager.LoadSceneAsync
,您将能够显示加载状态.
You need to use SceneManager.LoadSceneAsync
if you don't want this to block Unity while loading so many scenes. By using SceneManager.LoadSceneAsync
, you will be able to show the loading status.
创建一个场景,并确保该场景先于其他场景加载.从那里,您可以从0循环到场景的最大索引.您可以使用SceneManager.GetSceneByBuildIndex
从索引中检索Scene
,然后使用SceneManager.SetActiveScene
激活刚刚检索的场景.
Create a scene and make sure that this scene loads before any other scene. From there you can loop from 0 to the max index of your scene. You can use SceneManager.GetSceneByBuildIndex
to retrieve the Scene
from index then SceneManager.SetActiveScene
to activate the scene you just retrieved.
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
中的原因.
You can the enableScene(int index)
to enable the scene. Note that only one scene can be loaded at a time and you must activate them in the order you loaded them and finally, do not lose the reference of AsyncOperation
. This is why I stored them in a List
.
如果遇到问题,请尝试删除allScenes[index].allowSceneActivation = true;
和scene.allowSceneActivation = false;
.我有时看到这些会引起问题.
If you run into problems, try to remove allScenes[index].allowSceneActivation = true;
and scene.allowSceneActivation = false;
. I've seen these causing problems sometimes.
这篇关于在启动画面期间加载所有场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!