usingUnityEngine;
usingSystem.Collections;
usingSystem.Threading;
publicclassCoroutine:MonoBehaviour{
//Progress
//协程不是线程,协程不是异步执行,协程和Update()等生命周期方法一样,都是在主线程(mainThred)中执行;
//协程是分步执行,当遇到Yieldreturn就会挂起,直到条件满足才会继续执行后面的代码
//unity在每一帧都会去处理对象上的协程,处理协程在Update()之后,LateUpdate()之前也可能在LateUpdate之后,
//协程方法的参数不能用out,ref来修饰
//Yieldreturn0,null,/Wait.../StartCoroutine()/WWWW/UnityWebRequst
//floattime=3.0f;
voidStart(){
//StartCoroutine("CountDown");
StartCoroutine(Show("A"));
}
IEnumeratorShow(stringstr){
print(str);
yieldreturn0;
print(str);
yieldreturnnewWaitForSeconds(1.0f);
print(str);
yieldreturnStartCoroutine("CountDown");
print(str);
}
voidUpdate(){
}
//倒计时
//publicvoidCountDown(){
//time-=Time.deltaTime;
//if(time<=0){
//Debug.Log("finished");
//}
//}
IEnumeratorCountDown(){
print("bafore3seconds");
for(floattime=3.0f;time>=0;time-=Time.deltaTime){
yieldreturn0;
}
print("after3seconds");
}
}