unity update 协程_Unity协程,停止协程及yield return使用_019
public void Start()
{
//開啟協(xié)程
Coroutine testCoroutine = StartCoroutine(Test());
//停止指定協(xié)程
StopCoroutine(testCoroutine);
//協(xié)程可以同時開啟多個
StartCoroutine("Test");
//經(jīng)實測,StopCoroutine("Test")只能停止StartCoroutine("Test")開啟的協(xié)程,對StartCoroutine(Test())開啟的協(xié)程無效
StopCoroutine("Test");
//停止本腳本內(nèi)所有協(xié)程
StopAllCoroutines();
}
IEnumerator Test()
{
//等待下一幀Update之后,繼續(xù)執(zhí)行后續(xù)代碼
yield return null;
//等待在所有相機和GUI渲染之后,直到幀結(jié)束,繼續(xù)執(zhí)行后續(xù)代碼
yield return new WaitForEndOfFrame();
//等待下一個FixedUpdate之后,繼續(xù)執(zhí)行后續(xù)代碼
yield return new WaitForFixedUpdate();
//等待3秒之后,繼續(xù)執(zhí)行后續(xù)代碼,使用縮放時間暫停協(xié)程執(zhí)行達到給定的秒數(shù)
yield return new WaitForSeconds(3.0f);
//等待3秒之后,繼續(xù)執(zhí)行后續(xù)代碼,使用未縮放的時間暫停協(xié)程執(zhí)行達到給定的秒數(shù)
yield return new WaitForSecondsRealtime(3.0f);
//等待直到Func返回true,繼續(xù)執(zhí)行后續(xù)代碼
//yield return new WaitUntil(System.Func);
yield return new WaitUntil(() => true);
//等待直到Func返回false,繼續(xù)執(zhí)行后續(xù)代碼
//yield return new WaitWhile(System.Func);
yield return new WaitWhile(() => false);
//等待新開啟的協(xié)程完成后,繼續(xù)執(zhí)行后續(xù)代碼,可以利用這一點,實現(xiàn)遞歸
yield return StartCoroutine(Test());
//for循環(huán)
for (int i = 0; i < 10; i++)
{
Debug.Log(i);
yield return new WaitForSeconds(1);
}
//while循環(huán),while(true):如果循環(huán)體內(nèi)有yield return···語句,不會因為死循環(huán)卡死
int j = 0;
while (j < 10)
{
j++;
Debug.Log(j);
yield return new WaitForSeconds(1);
}
//終止本協(xié)程
yield break;
}
分享一位大大的翻譯博文,關(guān)于同步等待、同步協(xié)程、異步協(xié)程、并行協(xié)程,圖文說明非常清晰~
總結(jié)
以上是生活随笔為你收集整理的unity update 协程_Unity协程,停止协程及yield return使用_019的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php提交raw_PHP中如何POST提
- 下一篇: bootstrap 栅栏剧中_Boots