Kevin_meta 2022-10-18 13:15 采纳率: 11.1%
浏览 74

Unity 异步加载程序卡死

**异步加载时,加载到90%程序无响应,不是每次都会出现,有时一直不出现,一旦出现了可能好几次重启程序重启电脑都好不了。
代码如下:
**
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/*
异步加载控制
*/
public class AsyncLoading_Ctl : MonoBehaviour
{
Image fillImage;
TextMeshProUGUI progressTxt;
AsyncOperation operation;

private void Start()
{
    fillImage = transform.Find("FillImage").GetComponent<Image>() ;
    progressTxt = transform.Find("ProgressTxt").GetComponent<TextMeshProUGUI>();
    StartCoroutine("LoadScene");
    //判断进入游戏
    AppConst.isLogin = true;
}

IEnumerator LoadScene()
{
    yield return new WaitForSeconds(2);
    operation = SceneManager.LoadSceneAsync("MainScene");
    //while (true)//偶尔卡死
    //{
    //    fillImage.fillAmount = operation.progress;
    //    progressTxt.text = operation.progress * 100 + "%";
    //    yield return null;
    //}
    while (!operation.isDone)//偶尔卡死
    {
        fillImage.fillAmount = operation.progress;
        progressTxt.text = operation.progress * 100 + "%";
        yield return null;
    }
}

}

  • 写回答

1条回答 默认 最新

  • 带酒书生 2022-10-18 14:58
    关注

    我建议你换个写法operation 设为false,当operation progress大于0.9时修改为true自动跳转。AsyncOperation 这个类本身就有点奇怪,往往0.9左右就完成了,不设为false就会自动跳转,但这时候的isDone还是false,应该是跟你的while循环冲突了?

    评论

报告相同问题?

问题事件

  • 创建了问题 10月18日