Unity 协程
1. 協(xié)程不是多線程,見官方圖,它發(fā)生在LateUpdate()之后的
2. 定義協(xié)程的方法,寫個(gè)方法,它必須返回IEnumerator 接口,可帶參數(shù),也可以不帶參數(shù);
3. 啟動(dòng)協(xié)程StartCoroutine方法,
StartCoroutine("方法名")或StartCoroutine("方法名",相應(yīng)的參數(shù))
StarrtCoruntine(方法名())
4.停止協(xié)程
StopCoroutine("方法名"); 或StopAllCoroutines();
5.協(xié)程中常用的一些語句:
IEnumerator Test()
{
//等待下一幀Update之后,繼續(xù)執(zhí)行后續(xù)代碼
yield return null;
//等待在所有相機(jī)和GUI渲染之后,直到幀結(jié)束,繼續(xù)執(zhí)行后續(xù)代碼
yield return new WaitForEndOfFrame();
//等待下一個(gè)FixedUpdate之后,繼續(xù)執(zhí)行后續(xù)代碼
yield return new WaitForFixedUpdate();
//等待3秒之后,繼續(xù)執(zhí)行后續(xù)代碼,使用縮放時(shí)間暫停協(xié)程執(zhí)行達(dá)到給定的秒數(shù)
yield return new WaitForSeconds(3.0f);
//等待3秒之后,繼續(xù)執(zhí)行后續(xù)代碼,使用未縮放的時(shí)間暫停協(xié)程執(zhí)行達(dá)到給定的秒數(shù)
yield return new WaitForSecondsRealtime(3.0f);
//等待直到Func返回true,繼續(xù)執(zhí)行后續(xù)代碼
//yield return new WaitUntil(System.Func<bool>);
yield return new WaitUntil(() => true);
//等待直到Func返回false,繼續(xù)執(zhí)行后續(xù)代碼
//yield return new WaitWhile(System.Func<bool>);
yield return new WaitWhile(() => false);
//等待新開啟的協(xié)程完成后,繼續(xù)執(zhí)行后續(xù)代碼,可以利用這一點(diǎn),實(shí)現(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ì)因?yàn)樗姥h(huán)卡死
int j = 0;
while (j < 10)
{
j++;
Debug.Log(j);
yield return new WaitForSeconds(1);
}
//終止協(xié)程
yield break;
}
總結(jié)
- 上一篇: ABAP GUI Text Edit
- 下一篇: SAP CRM WebClient UI