Unity中的LoadScene()函数何时更改场景? [英] When does the LoadScene() function in Unity change the scene?

查看:1472
本文介绍了Unity中的LoadScene()函数何时更改场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用函数LoadScene()时 它会立即切换场景,还是仅仅表明需要更改场景? LoadScene()的文档没有说明.

When you invoke the function LoadScene() does it immediately switch scenes, or does it simply signal that the scene needs to be changed? The documentation for LoadScene() does not say.

我正在处理的特定示例如下:

The specific example I am working with looks like this:

LoadScene(levelName);
ItemBox newBox = (ItemBox)Instantiate(...);

因此,使用上面的代码,newBox将存在于我们刚刚加载的场景中,还是会在旧场景中创建,然后在加载新关卡时销毁.

So with the above code, would the newBox exist in the scene we just loaded, or would it get created in the old scene, then destroyed when the new level is loaded.

推荐答案

 UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay");

是的,它立即"执行-也就是说 s 同步.

and yes it does it "instantly" -- that is to say synchronously.

换句话说,它停止"在那行代码,等待直到加载整个场景(即使花费几秒钟),然后新场景开始.

In other words, it "stops" at that line of code, waits until it loads the whole scene (even if that takes some seconds) and then the new scene begins.

请勿使用您在问题中提到的旧命令.

Do not use the old command you mention in the question.

请注意,Unity还具有 a 同步加载功能.它在后台缓慢加载新场景".

Note that Unity also has the ability to do async loading .. it "slowly loads the new scene in the background".

但是:我建议您仅使用普通的"LoadScene".重要的是可靠性和简单性. 如果机器在加载关卡时仅停止"了几秒钟,则用户不要介意.

However: I encourage you to only use ordinary "LoadScene". All that matters is reliability and simplicity. Users simply don't mind if the machine just "stops" for a few seconds while a level loads.

(每次我在电视上单击"Netflix"时,电视都会花费一些时间.没人在乎-这是正常的.)

(Every time I click "Netflix" on my TV, it takes some time for the TV to do that. Nobody cares - it is normal.)

但是,如果您确实想在后台加载,请按以下步骤操作...

But if you do want to load in the background, here's how...

public void LaunchGameRunWith(string levelCode, int stars, int diamonds)
    {
    .. analytics
    StartCoroutine(_game( levelCode, superBombs, hearts));
    }

private IEnumerator _game(string levelFileName, int stars, int diamonds)
    {
    // first, add some fake delay so it looks impressive on
    // ordinary modern machines made after 1960
    yield return new WaitForSeconds(1.5f);

    AsyncOperation ao;
    ao = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("Gameplay");

    // here's exactly how you wait for it to load:
    while (!ao.isDone)
        {
        Debug.Log("loading " +ao.progress.ToString("n2"));
        yield return null;
        }

    // here's a confusing issue. in the new scene you have to have
    // some sort of script that controls things, perhaps "NewLap"
    NewLap newLap = Object.FindObjectOfType< NewLap >();
    Gameplay gameplay = Object.FindObjectOfType<Gameplay>();

    // this is precisely how you conceptually pass info from
    // say your "main menu scene" to "actual gameplay"...
    newLap.StarLevel = stars;
    newLap.DiamondTime = diamonds;

    newLap.ActuallyBeginRunWithLevel(levelFileName);
    }

注意:该脚本回答了当玩家将游戏打到实际游戏场景"时如何从主菜单"传递信息的问题.

Note: that script answers the question of how you pass information "from your main menu" when the player hits play "on to the actual game play scene".

这篇关于Unity中的LoadScene()函数何时更改场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆