使用StopCoroutine(“名字”)或StopCoroutine(方法名),無法立刻殺死協程,還是會執行到結尾。
StopCoroutine()用string參數,只可以關閉StartCoroutine()方法使用相同string參數開啓的攜程。
需要創建協程變量,並在StartCorutine(),開啓協程時賦值。
StopCoroutine(協程變量名),即可殺死此協程。
//開啓協程
Coroutine myCoroutine = StartCoroutine(Test());
//關閉協程
StopCoroutine(myCoroutine);
IEnumerator Test()
{
yield return new WaitForSeconds(1f);
Debug.Log("test");
}