Unity stop and start coroutines

馋奶兔 提交于 2019-12-25 08:59:38

问题


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:


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?



来源:https://stackoverflow.com/questions/35735319/unity-stop-and-start-coroutines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!