问题
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