我有一個方法來啓動協程,並應該停止它並重新啓動它之前重新設置一些事情。Unity StopCoroutine不工作
private Coroutine wholeTutorialRoutine;
public void RunWholeTutorial()
{
tutorialText.text = "";
StopAllCoroutines();
if(wholeTutorialRoutine != null)
{
StopCoroutine(wholeTutorialRoutine);
}
wholeTutorialRoutine = StartCoroutine(WholeTutorial());
}
private IEnumerator WholeTutorial()
{
// Wait until after we are done showing this dialouge
yield return StartCoroutine(ShowDialougForSeconds("tap_to_kill", 5f));
yield return new WaitForSeconds(5f);
yield return StartCoroutine(ShowDialougForSeconds("larger_enemies", 5f));
yield return new WaitForSeconds(5f);
yield return StartCoroutine(ShowDialougForSeconds("press_button", 7f));
yield return new WaitForSeconds(3f);
yield return StartCoroutine(ShowDialougForSeconds("button_colors", 5f));
}
private IEnumerator ShowDialougForSeconds(string diagID, float time)
{
SetText(diagID);
tutorialText.GetComponent<Animator>().SetTrigger("FadeIn");
yield return new WaitForSeconds(time);
tutorialText.GetComponent<Animator>().SetTrigger("FadeOut");
}
wholeTutorialRoutine
是Coroutine
類型的私有字段。
我覺得有些事情發生在那些WaitForSeconds
調用中,但我不確定是什麼。
RunWholeTutorial()
連接到一個按鈕,所以我想停止當前的教程,並重新開始,如果用戶反覆按下它。
目前發生的情況似乎是協程運行在另一個之上。
您是否嘗試將呼叫改變爲if語句下的StopAllCoroutines? – z3nth10n
是的,我沒有改變任何東西 – BenjaFriend
刪除'StopAllCoroutines'並看看會發生什麼。這是很好的顯示'wholeTutorialRoutine'是如何聲明的 – Programmer