**异步加载时,加载到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
() ;
progressTxt = transform.Find("ProgressTxt").GetComponent();
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;
}
}
}
我建议你换个写法operation 设为false,当operation progress大于0.9时修改为true自动跳转。AsyncOperation 这个类本身就有点奇怪,往往0.9左右就完成了,不设为false就会自动跳转,但这时候的isDone还是false,应该是跟你的while循环冲突了?