C# 如何等待场景卸载?
当我单击一个按钮时,它正在调用方法ActivatePlayer:C# 如何等待场景卸载?,c#,unity3d,C#,Unity3d,当我单击一个按钮时,它正在调用方法ActivatePlayer: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class LoadSceneOnClick : MonoBehaviour { private Scene scene; private void Start()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadSceneOnClick : MonoBehaviour
{
private Scene scene;
private void Start()
{
scene = SceneManager.GetActiveScene();
StartCoroutine(WaitForSceneUnLoad(SceneManager.GetSceneByName("The Space Station")));
}
public IEnumerator WaitForSceneUnLoad(Scene scene)
{
return null;
}
public void ActivatePlayer()
{
SceneManager.UnloadSceneAsync(0);
Cursor.visible = false;
GameControl.player.SetActive(true);
}
}
但是现在我想等待UnloadSceneAsync完成第一行:
SceneManager.UnloadSceneAsync(0);
只有在完成卸载后,才可完成其余两行:
Cursor.visible = false;
GameControl.player.SetActive(true);
问题是,当我单击按钮时,我仍然会看到场景的一些gui元素。“索引中的场景”是主菜单,当我单击该按钮一秒钟甚至不到一秒钟时,我会看到主菜单文本的一部分。在卸载主菜单完成之前,它似乎正在激活播放器。因为主菜单和播放器所在的场景都有摄像头,所以我先看一下主菜单。尝试将脚本放入IEnumerator
public void ActivatePlayer()
{
StartCoroutine(StartActivatePlayer());
}
IEnumerator StartActivatePlayer()
{
AsyncOperation ao = SceneManager.UnloadSceneAsync(0);
yield return ao;
Cursor.visible = false;
GameControl.player.SetActive(true);
}
希望能有所帮助