当我点击一个按钮时,它在On Click中调用方法ActivatePlayer: using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;public class LoadSceneOnClick : MonoBehaviour{ private
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);
问题是,当我单击按钮时,我仍然会看到场景index0的第二个gui元素.索引中的场景是主菜单,当我单击按钮一秒钟时,我会看到主菜单文本的一部分.在卸载主菜单之前,它似乎正在激活播放器.由于主菜单和播放器都有摄像头的场景,我看到主菜单一秒钟.
尝试将您的脚本放在IEnumerator中public void ActivatePlayer() { StartCoroutine(StartActivatePlayer()); } IEnumerator StartActivatePlayer() { AsyncOperation ao = SceneManager.UnloadSceneAsync(0); yield return ao; Cursor.visible = false; GameControl.player.SetActive(true); }
希望它有所帮助.