Unity中的LoadScene()函数何时更改场景?

mi保

当您调用函数LoadScene()时,它会立即切换场景吗,还是只是发出信号表明需要更改场景?LoadScene()的文档未说明。

我正在使用的特定示例如下所示:

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

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

胖子

它的

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

是的它是“瞬间” -也就是小号ynchronously。

换句话说,它在该行代码处“停止”,等待直到加载整个场景(即使需要几秒钟),然后新场景开始。

不要使用问题中提到的旧命令。

需要注意的是团结也有能力做一个同步加载..它“慢慢地在后台加载新场景”。

但是:我建议您仅使用普通的“ LoadScene”。重要的是可靠性和简单性。用户根本不介意在水平仪加载时机器仅“停止”几秒钟。

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

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

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);
    }

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章