繁体   English   中英

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

[英]When does the LoadScene() function in Unity change the scene?

当您调用函数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);
    }

注意:当播放器“播放到实际的游戏场景”时,该脚本会回答“如何从主菜单”传递信息的问题。

也许它改变了。 来自文档:

使用SceneManager.LoadScene时,加载不会立即发生,它会在下一帧中完成。

我一直在做与海报完全相同的事情,它在旧场景中实例化。

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM