Unity无法设置ActiveScene [英] Unity cannot set ActiveScene

查看:464
本文介绍了Unity无法设置ActiveScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在切换场景:

SceneManager.LoadScene("Scene2");
Debug.Log("Current scene: " + SceneManager.GetActiveScene().name);

调试说:

Current scene: Scene1

我也尝试过这个:

SceneManager.LoadScene("Scene2");
StartCoroutine(WaitUntilEndOfFrame());
Debug.Log("Current scene: " + SceneManager.GetActiveScene().name);

private IEnumerator WaitUntilEndOfFrame() {

yield return new WaitForEndOfFrame();

}

仍然说

Current scene: Scene1.

我已经尝试过SetActiveScene(SceneManager.GetSceneByBuildId(1));

I've tried SetActiveScene(SceneManager.GetSceneByBuildId(1));

我尝试将其作为协程启动,我尝试了并等待5秒钟.即使场景在Unity中发生变化,ActiveScene仍然是Scene1.什么都没有改变活动场景...

I've tried launching that as a coroutine, I've tried that and waiting for 5 seconds. Still ActiveScene is Scene1, even though the scene changes in Unity. Nothing changes the active scene...

推荐答案

调用SceneManager.LoadScene("Scene2");时,场景直到下一帧才卸载.因此,如果紧随其后调用Debug.Log("Current scene: " + SceneManager.GetActiveScene().name);,它将返回当前场景的名称,而不是刚刚加载的场景.

When SceneManager.LoadScene("Scene2"); is called, the scene does not unload until next frame. So, if Debug.Log("Current scene: " + SceneManager.GetActiveScene().name); called right after that, it will return the name of the current scene not that scene is just loaded.

即使您使用协程并等待框架,也不会起作用,因为在框架之后,脚本和 GameObject 将被销毁,并且您的Debug.Log("Current scene: " + SceneManager.GetActiveScene().name);将无法运行

Even when you use coroutine and wait for a frame, that wouldn't work either because after a frame, the script and GameObject will be destroyed and your Debug.Log("Current scene: " + SceneManager.GetActiveScene().name); won't get to run.

不要在SceneManager.LoadScene("Scene2");之后立即调用SceneManager.GetActiveScene().name.将SceneManager.GetActiveScene().name放在AwakeStart函数中,您将能够在场景加载后获取场景名称.

Don't call SceneManager.GetActiveScene().name right after SceneManager.LoadScene("Scene2");. Put SceneManager.GetActiveScene().name in the Awake or Start function and you will be able to get scene name after the scene loads.

要做的另一件事是订阅 SceneManager.sceneLoaded 事件.现在,呼叫SceneManager.LoadScene("Scene2");.场景加载完成后,应调用已注册的函数.

Another thing to do to is subscribe to the SceneManager.sceneLoaded event. Now, call SceneManager.LoadScene("Scene2");. When scene is done loading, the registered function should be called.

void Start()
{
    SceneManager.LoadScene("Scene2");
}

void OnEnable()
{
    SceneManager.sceneLoaded += this.OnLoadCallback;
}

private void OnLoadCallback(Scene scene, LoadSceneMode sceneMode)
{
    UnityEngine.Debug.Log("Current scene: " + SceneManager.GetActiveScene().name);
}


最后,如果您不将场景添加到 Build Settings 中,则SceneManager.LoadScene("Scene2");甚至无法工作,因此您当前的场景仍将是您的当前场景.以下是如何将场景添加到构建设置.


Finally, if you don't add the scenes to the Build Settings, SceneManager.LoadScene("Scene2"); won't even work so your current scene will still be your current scene. Below is how to add scenes to the Build Settings.

这篇关于Unity无法设置ActiveScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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