c# – 如何等待场景卸载?
发布时间:2020-12-16 01:26:59 所属栏目:百科 来源:网络整理
导读:当我点击一个按钮时,它在On Click中调用方法ActivatePlayer: using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;public class LoadSceneOnClick : MonoBehaviour{ private Scene scene; pri
当我点击一个按钮时,它在On Click中调用方法ActivatePlayer:
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); } 希望它有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |