C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 如何等待场景卸载?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我点击一个按钮时,它在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);
}

希望它有所帮助.

大佬总结

以上是大佬教程为你收集整理的c# – 如何等待场景卸载?全部内容,希望文章能够帮你解决c# – 如何等待场景卸载?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: