Unity終止協程的問題

2022-02-03 02:45:08 字數 568 閱讀 6411

最近在專案開發的時候發現,呼叫stopcoroutine()來終止乙個正在執行的協程的時候,無法終止協程。然後通過查資料發現了一下兩種辦法:

比如,我有乙個協程ienumerator moveanimation(),啟動時要用startcoroutine("moveanimation"),終止的時候用stopcoroutine("moveanimation"),才能正確終止協程。

要想傳參,可以使用startcoroutine("moveanimation", object)來傳參。

定義乙個coroutine型別的變數coroutine,在startcoroutine(moveanimation())後接收返回的值:

coroutine=startcoroutine("moveanimation");

然後終止協程的時候利用stopcoroutine(coroutine)來終止。

Unity關閉協程,無法立刻殺死協程的問題

使用stopcoroutine 名字 或stopcoroutine 方法名 無法立刻殺死協程,還是會執行到結尾。stopcoroutine 用string引數,只可以關閉startcoroutine 方法使用相同string引數開啟的攜程。需要建立協程變數,並在startcorutine 開啟協程時...

Unity 協程詳解

class myiter ienumerable myiter iter newmyiter foreach int num in iter 我們需要讓資料結構支援foreach遍歷,所以要繼承ienumerable介面,實現getenumerator方法返回乙個迭代器,那麼這裡就很奇怪,返回值明明...

Unity中的協程

1.建立協程 迭代器 ienumerator private ienumerator test 裡面可以使用 yield return new waitforsecond 1 等待1s後執行後面的 yield return new waitforseconds 0.3f 等待0.3秒,一段指定的時間...