카테고리 없음

StopCoroutine 사용 시 주의사항

blueasa 2014. 5. 12. 15:46

참조 : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StopCoroutine.html


void StopCoroutine(string methodName);


StopCoroutine은 위와같은 형태이다. Stop시킬 Method를 string 형태로 해야한다.


생각없이 쓰려다가 안돼서 레퍼런스를 다시 보니 이전에도 봤던기억이 있는 아래와 같은 문장이..;;

(역시 사람은 망각의 동물..)


Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine.


StopCoroutine을 쓰려면 StartCoroutine도 string형태의 method name으로 실행해야 한다.


또 까먹을까봐 정리해놓음.



[추가 Tip]

StartCoroutine으로 같은 이름의 함수를 여러번 실행해도, StopCoroutine을 해당 함수명으로 한 번만 실행하면 같은 함수명을 가진 Coroutine이 모두 중지된다.


ex)

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    IEnumerator Start()
    {
        StartCoroutine("DoSomething", 2.0F);
        StartCoroutine("DoSomething", 4.0F);
        yield return new WaitForSeconds(1f);
        StopCoroutine("DoSomething"); // 한 번만 실행해도 위에서 실행된 DoSomething Coroutine 2개가 모두 취소된다.
    }
    IEnumerator DoSomething(float someParameter)
    {
        while (true)
        {
            print("DoSomething Loop" + someParameter);
            yield return null;
        }
    }
}
반응형