Unity3D之协程(Coroutines & Yield )

<span style="font-family: Arial; font-size: 18px; line-height: 26px;">yield return可以看做是一种特殊的return,会返回到父类继续执行,但是yield return后面的类型或方法会有一个执行条件,当条件满足时会回调包含yield的子函数,例如下面代码</span>
<span style="font-family: Arial; font-size: 18px; line-height: 26px;"><span style="font-family: Arial; font-size: 18px; line-height: 26px;">例1:</span>
</span>
<span style="font-family: Arial; font-size: 18px; line-height: 26px;"><span style="font-family: Arial; font-size: 18px; line-height: 26px;"></span></span><pre name="code" class="cpp">using UnityEngine;
using System.Collections;

public class YieldExample : MonoBehaviour {

	// Use this for initialization
	void Start () 
    {
        print("Starting:" + Time.time);  
        StartCoroutine(DelayTime(12.0f));
        print("Before WaiAndPrint:" + Time.time); ;
	}
	
	// Update is called once per frame
	void Update () {
       
	}

    IEnumerator DelayTime(float waitTime)
    {
        yield return new WaitForSeconds(waitTime); //等待3秒
        print("WaitAndPrint:" + Time.time);      
    }

}
<span style="font-family: Arial; font-size: 18px; line-height: 26px;">在执行yield return new WaitForSeconds(waitTime)时暂停的条件没有满足,故返回到start函数中继续执行,直到满足条件后再回调WaitAndPrint,所以输出为:</span><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><span style="font-family: Arial; font-size: 18px; line-height: 26px;">Starting:0</span><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><span style="font-family: Arial; font-size: 18px; line-height: 26px;">Before WaiAndPrint:0</span><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><span style="font-family: Arial; font-size: 18px; line-height: 26px;">WaitAndPrint:2.12291</span><br style="font-family: Arial; font-size: 18px; line-height: 26px;" /><span style="font-family: Arial; font-size: 18px; line-height: 26px;">例2:</span>
<pre name="code" class="cpp">using UnityEngine;
using System.Collections;

public class YieldExample : MonoBehaviour {

	// Use this for initialization
    IEnumerator Start() 
    {
        print("Starting:" + Time.time);  
        yield return StartCoroutine(DelayTime(12.0f));
        print("done:" + Time.time); ;
	}
	
	// Update is called once per frame
	void Update () {
       
	}

    IEnumerator DelayTime(float waitTime)
    {
        yield return new WaitForSeconds(waitTime); //等待3秒
        print("WaitAndPrint:" + Time.time);      
    }

}

因为start为顶级函数,所以会阻塞在这里,直到StartCoroutine(WaitAndPrint(2.0F))执行完毕,输出为:


starting:0

WaitAndPrint:2.00315

done:2.00315
 
 
 
 
 
 
<span style="font-size: 14px; line-height: 26px; font-family: Tahoma, Helvetica, SimSun, sans-serif; color: rgb(68, 68, 68);"><span style="line-height: 21px;"><span style="font-size: 18px;">写游戏代码,往往最终需要代码为连续的事件.结果会像这样:
[它可以实现将一段程序延迟执行或者将其各个部分分布在一个时间段内连续执行。]

</span></span></span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 531px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">></span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">private</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> state = 0;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Update()  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (state == 0)   </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        {  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//做步骤0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                state = 1;  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (state == 1)   </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        {  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 做步骤1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                state = 2;  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// ...</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">

往往使用yield语句更为方便.yield语句是一个特殊的返回类型,它确保函数从yield语句的下一行继续执行.


</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 1032px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">></span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">while</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">true</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 做步骤0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 0;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">         <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 等待一帧</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 做步骤1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 2;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">         <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 等待两帧</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// ...</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">

你也可以传递时间值到yield语句,Update函数会在yield结束后执行下一语句.

</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 1363px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>  </span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// do something</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> WaitForSeconds  (5.0);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//等待5秒</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// do something more...  </span></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">
你可以入栈并连接协程.


这个例子将执行Do,但是do函数之后的print指令会立刻执行.


</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 1682px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>Do ();  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"This is printed immediately"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">IEnumerator  Do ()  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Do now"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> WaitForSeconds  (2);        </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Do 2 seconds later"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">

这个例子将执行Do,并等待,直到Do完成再执行其他语句.【注:这里的等待是把线程时间交给其他任务,而不是阻塞式等待】


</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 2047px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">></span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// 启动协程</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> StartCoroutine(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Do"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Also after 2 seconds"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">Console.WriteLine (<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"这个print将在Do协程执行完以后显示。"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">IEnumerator  Do ()  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{        </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Do now"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> WaitForSeconds  (2);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">Console.WriteLine(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Do 2 seconds later"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"></span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">

任何事件处理程序都可以是协同程序 。


注意你不能在Update或FixedUpdate函数内使用yield,但是你能使用 StartCoroutine  开始一个函数.


查看 YieldInstruction , WaitForSeconds , WaitForFixedUpdate , Coroutine  and MonoBehaviour.StartCoroutine  可以获得更多使用yield的信息.
yield return可以看做是一种特殊的return,会返回到父类继续执行,但是yield return后面的类型或方法会有一个执行条件,当条件满足时会回调包含yield的子函数,例如下面代码
例1:


</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 2700px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">></span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Start () {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Starting:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        StartCoroutine(WaitAnPrint(2.0F));  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"Before WaiAndPrint:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">IEnumerator WaitAndPrint(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">float</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> waitTime)  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    {  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> WaitForSeconds(waitTime);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"WaitAndPrint:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);      </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"></span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">

在执行yield return new WaitForSeconds(waitTime)时暂停的条件没有满足,故返回到start函数中继续执行,直到满足条件后再回调WaitAndPrint,所以输出为:


Starting:0


Before WaiAndPrint:0


WaitAndPrint:2.12291


例2:


</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 3755px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"font-size:18px;"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>IEnumerator Start()  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    {  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"starting:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> StartCoroutine(WaitAndPrint(2.0F));  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"done:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">IEnumerator WaitAndPrint(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">float</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> waitTime)  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    {  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        yield <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> WaitForSeconds(waitTime);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        print(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"WaitAndPrint:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + Time.time);      </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }</span>  </span></li></ol></div><span style="font-family: Arial; line-height: 26px; font-size: 18px;">


因为start为顶级函数,所以会阻塞在这里,直到StartCoroutine(WaitAndPrint(2.0F))执行完毕,输出为:


starting:0

WaitAndPrint:2.00315

done:2.00315
</span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 936.53125px; overflow: auto; padding-top: 1px; line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">view plain</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">copy</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">print</a><a target=_blank href="http://blog.csdn.net/jjiss318/article/details/7447421#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial;">?</a><div style="position: absolute; left: 594px; top: 4664px; width: 29px; height: 14px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><pre name=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"code"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"csharp"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">><pre name=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"code"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"csharp"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">><pre name=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"code"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"csharp"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">><p></p><pre></pre>  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><pre></pre>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(245, 250, 226); color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"></pre></pre></pre>  </span></li></ol></div>
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(Unity3D之协程(Coroutines & Yield ))