c# - 在启动画面期间加载所有场景

标签 c# splash-screen unity3d

我的手机 2D Unity 游戏中有多个场景,我想在启动画面中加载我的所有场景,这样场景传递就会很流畅。我怎样才能做到这一点 ?

如果我这样做,是否需要更改“Application.LoadScene()”方法,我可以使用什么方法?

最佳答案

do I need to change "Application.LoadScene()" method, and what method can I use ?

如果您不想在加载这么多场景时阻止 Unity,则需要使用 SceneManager.LoadSceneAsync。通过使用 SceneManager.LoadSceneAsync,您将能够显示加载状态。

I want to load all my scenes in splash screen

创建一个场景并确保该场景在任何其他场景之前加载。从那里你可以从 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;。我有时会看到这些问题。

关于c# - 在启动画面期间加载所有场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45742735/

相关文章:

android - 为什么Android 8.1看不到资源?

android - Android 上触摸释放时触发 OnPointerExit() 的解决方案?

c# - WP7图像处理API

c# - Activator.CreateInstance() 和 typeof(T).InvokeMember() 与 BindingFlags.CreateInstance 之间的区别

java - 进度条: how to create deep copy of original object

android - 启动画面 api 正在显示 android 12 的操作栏

unityscript - 我可以在 Unity 5 中知道谁实例化了对象吗?

c# - 检测可能的射弹与盾牌碰撞

c# - Windows Phone 8.1 中的相机声音

c# - C# 是否有办法在小范围内模拟软件事务内存?