Unity stop and start coroutines

2019-09-10 11:37发布

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

1条回答
2楼-- · 2019-09-10 11:45
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?

查看更多
登录 后发表回答