gpt4 book ai didi

c# - Unity StopCoroutine 不工作

转载 作者:行者123 更新时间:2023-11-30 23:09:06 27 4
gpt4 key购买 nike

我有一个启动协程的方法,应该在再次启动之前停止它并重置一些东西。

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");
}

wholeTutorialRoutineCoroutine 类型的私有(private)字段。

我觉得那些 WaitForSeconds 调用发生了一些奇怪的事情,但我不太确定是什么。

RunWholeTutorial() 连接到一个按钮,所以我想停止当前教程,如果用户一遍又一遍地按下它,我会重新开始。

目前发生的事情似乎是协同程序在彼此之上运行。

最佳答案

使用 IEnumerator 而不是 CoroutineRunWholeTutorial 协程函数的实例 once 存储在 开始功能。然后,您可以使用该 IEnumerator 变量启动和停止它。

private IEnumerator wholeTutorialRoutine;

void Start()
{
wholeTutorialRoutine = WholeTutorial();
}

public void RunWholeTutorial()
{
tutorialText.text = "";

if (wholeTutorialRoutine != null)
{
StopCoroutine(wholeTutorialRoutine);
}

StartCoroutine(wholeTutorialRoutine);
}

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");
}

关于c# - Unity StopCoroutine 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com