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