Unity停止和启动协程 [英] Unity stop and start coroutines

查看:770
本文介绍了Unity停止和启动协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何拥有一个可以播放协程的功能,但是首先要检查它是否已经在运行,如果有的话就将其停止?

How can I have a function which will play a coroutine, but first check if it is already running to stop it if it is?

编辑:抱歉,我没有看到有人已经问过/回答了这个问题,这是我被困了一段时间的原因

EDIT: Sorry I didnt see that someone had already asked/answered this and it was something I was stuck on for a bit

推荐答案

public class Example : MonoBehaviour 
{
  [SerializeField] // Allow the variable to be edited in editor
  private float parameterValue; 
  private IEnumerator coroutine;

  private IEnumerator CoroutineA(float _parameter)
  { // do something, usually for _parameter time }

  private void StartDoSomething () 
  { 
    if (coroutine != null)
      StopCoroutine(coroutine); // no overlaps on timers etc

    coroutine = CoroutineA(parameterValue);
    StartCoroutine(coroutine);
  }
}

或查看链接的答案: 如何停止协同程序?

OR see linked answer: How to stop co-routine?

这篇关于Unity停止和启动协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆