当我的游戏完成异步功能时,如何启用功能?
Posted
技术标签:
【中文标题】当我的游戏完成异步功能时,如何启用功能?【英文标题】:How do I enable a function when my game is done doing an asynchronous function? 【发布时间】:2022-01-15 18:25:37 【问题描述】:这让我作为初学者/学习编码员感到沮丧。我一直在努力做到这一点,所以当我的游戏使用异步方法完成加载时,它会加载一个按钮,以便您继续。相反,它完全忽略了该功能,导致按钮仍然在游戏中,并且当您过早按下它时会出现故障。我的代码就在这里;我该如何解决这个问题?
IEnumerator LoadSceneAsynchronously(int levelIndex)
AsyncOperation operation = SceneManager.LoadSceneAsync(levelIndex);
while (!operation.isDone)
Debug.Log(operation.progress);
loading.GetComponent<Animator>().Play("loading");
text.GetComponent<Animator>().Play("idle");
yield return null;
if (!operation.isDone)
yield return new WaitForSeconds(5);
loading.GetComponent<Animator>().Play("loading disappear");
text.GetComponent<Animator>().Play("appear text");
text.GetComponent<Animator>().Play("blink text")
yield return null;
public void DestroyLoading()
gameObject.SetActive(false);
GameIsDoneLoading = true;
void Update()
if (Input.GetKeyDown(KeyCode.Space) && !GameIsDoneLoading == true)
Debug.Log("Space key was pressed.");
text.GetComponent<Animator>().Play("disappear text");
logo.GetComponent<Animator>().Play("fpsa logo disappear");
【问题讨论】:
【参考方案1】:当您在协程中“让步”时,代码执行将返回到下一帧中完全相同的位置,因此在上面的 while 循环中,它只是每帧检查该条件,然后让步到下一帧。我相信你想做的是……
IEnumerator LoadSceneAsynchronously(int levelIndex)
// start the async loading
AsyncOperation operation = SceneManager.LoadSceneAsync(levelIndex);
// start playback of your animations in the scene
loading.GetComponent<Animator>().Play("loading");
text.GetComponent<Animator>().Play("idle");
// now wait for the scene to fully load
while (!operation.isDone)
Debug.Log(operation.progress);
yield return null;
// after yielding, we return here and repeat the while
// loop on the next frame
// we have broken free of the while loop so the level has now fully loaded
// and operation.isDone is now true
// play some other animations to fade out the loading system?
loading.GetComponent<Animator>().Play("loading disappear");
text.GetComponent<Animator>().Play("appear text");
text.GetComponent<Animator>().Play("blink text")
// I'm assuming these animations take about 5 seconds to complete?
// so just wait in here for 5
yield return new WaitForSeconds(5);
// loading level and animations all done...proceed to games
DestroyLoading();
【讨论】:
以上是关于当我的游戏完成异步功能时,如何启用功能?的主要内容,如果未能解决你的问题,请参考以下文章