用一个下面这样的函数切换一个小场景,总是卡90%,但是场景实际上已经加载完成。
public IEnumerator SwitchSceneAsync(int sceneIndex=0)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
operation.allowSceneActivation = false;
while (operation.progress<0.9f)
{
loadingCanvas.transform.GetChild(0).GetComponentInChildren<TextMeshProUGUI>().text = string.Format("{0:P0}", operation.progress);
yield return new WaitForEndOfFrame();
}
loadingCanvas.transform.GetChild(0).GetComponentInChildren<TextMeshProUGUI>().text = "100%";
yield return new WaitForEndOfFrame();
operation.allowSceneActivation = true;
yield return new WaitUntil(()=>operation.isDone);
loadingCanvas.transform.GetComponent<RectMask2D>().enabled = true;
Debug.Log("场景加载完成");
yield break;
}
后来发现是因为在原场景中的被Destory物体的脚本中来StartCoroutine,导致加载到一半协程被干掉了....