Unity3D之协程(Coroutines & Yield )
来源:互联网 发布:四川大学土木工程知乎 编辑:程序博客网 时间:2024/05/21 13:15
写游戏代码,往往最终需要代码为连续的事件.结果会像这样:
[它可以实现将一段程序延迟执行或者将其各个部分分布在一个时间段内连续执行。]
你可以入栈并连接协程.
这个例子将执行Do,但是do函数之后的print指令会立刻执行.
这个例子将执行Do,并等待,直到Do完成再执行其他语句.【注:这里的等待是把线程时间交给其他任务,而不是阻塞式等待】
任何事件处理程序都可以是协同程序 。
注意你不能在Update或FixedUpdate函数内使用yield,但是你能使用 StartCoroutine 开始一个函数.
查看 YieldInstruction , WaitForSeconds , WaitForFixedUpdate , Coroutine and MonoBehaviour.StartCoroutine 可以获得更多使用yield的信息.
yield return可以看做是一种特殊的return,会返回到父类继续执行,但是yield return后面的类型或方法会有一个执行条件,当条件满足时会回调包含yield的子函数,例如下面代码
例1:
在执行yield return new WaitForSeconds(waitTime)时暂停的条件没有满足,故返回到start函数中继续执行,直到满足条件后再回调WaitAndPrint,所以输出为:
Starting:0
Before WaiAndPrint:0
WaitAndPrint:2.12291
例2:
因为start为顶级函数,所以会阻塞在这里,直到StartCoroutine(WaitAndPrint(2.0F))执行完毕,输出为:
starting:0
WaitAndPrint:2.00315
done:2.00315
[它可以实现将一段程序延迟执行或者将其各个部分分布在一个时间段内连续执行。]
<span style="font-size:18px;">private int state = 0; void Update() { if (state == 0) { //做步骤0 state = 1; return; } if (state == 1) { // 做步骤1 state = 2; return; } // ... } </span>
往往使用yield语句更为方便.yield语句是一个特殊的返回类型,它确保函数从yield语句的下一行继续执行.
<ol start="1" class="dp-c" style="padding: 0px; border: none; color: rgb(92, 92, 92); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 26px; 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; color: inherit; line-height: 18px; background-color: rgb(245, 250, 226);"><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; font-weight: bold; background-color: inherit;">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; font-weight: bold; background-color: inherit;">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; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(245, 250, 226);"><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; color: inherit; line-height: 18px; background-color: rgb(245, 250, 226);"><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; font-weight: bold; background-color: inherit;">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; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(245, 250, 226);"><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; color: inherit; line-height: 18px; background-color: rgb(245, 250, 226);"><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; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(245, 250, 226);"><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; font-weight: bold; background-color: inherit;">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; color: inherit; line-height: 18px; background-color: rgb(245, 250, 226);"><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; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(245, 250, 226);"><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; color: inherit; line-height: 18px; background-color: rgb(245, 250, 226);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span> </span></li></ol>你也可以传递时间值到yield语句,Update函数会在yield结束后执行下一语句.
<span style="font-size:18px;"> // do something yield return WaitForSeconds (5.0); //等待5秒 // do something more... </span>
你可以入栈并连接协程.
这个例子将执行Do,但是do函数之后的print指令会立刻执行.
<span style="font-size:18px;">Do (); Console.WriteLine("This is printed immediately"); IEnumerator Do () { Console.WriteLine("Do now"); yield return new WaitForSeconds (2); Console.WriteLine("Do 2 seconds later"); } </span>
这个例子将执行Do,并等待,直到Do完成再执行其他语句.【注:这里的等待是把线程时间交给其他任务,而不是阻塞式等待】
<span style="font-size:18px;">// 启动协程 yield return StartCoroutine("Do"); Console.WriteLine("Also after 2 seconds"); Console.WriteLine ("这个print将在Do协程执行完以后显示。"); IEnumerator Do () { Console.WriteLine("Do now"); yield return new WaitForSeconds (2); Console.WriteLine("Do 2 seconds later"); } </span>
任何事件处理程序都可以是协同程序 。
注意你不能在Update或FixedUpdate函数内使用yield,但是你能使用 StartCoroutine 开始一个函数.
查看 YieldInstruction , WaitForSeconds , WaitForFixedUpdate , Coroutine and MonoBehaviour.StartCoroutine 可以获得更多使用yield的信息.
yield return可以看做是一种特殊的return,会返回到父类继续执行,但是yield return后面的类型或方法会有一个执行条件,当条件满足时会回调包含yield的子函数,例如下面代码
例1:
<span style="font-size:18px;">void Start () { print("Starting:" + Time.time); StartCoroutine(WaitAnPrint(2.0F)); print("Before WaiAndPrint:" + Time.time); } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint:" + Time.time); } </span>
在执行yield return new WaitForSeconds(waitTime)时暂停的条件没有满足,故返回到start函数中继续执行,直到满足条件后再回调WaitAndPrint,所以输出为:
Starting:0
Before WaiAndPrint:0
WaitAndPrint:2.12291
例2:
- <span style="font-size:18px;">IEnumerator Start()
- {
- print("starting:" + Time.time);
- yield return StartCoroutine(WaitAndPrint(2.0F));
- print("done:" + Time.time);
- }
- IEnumerator WaitAndPrint(float waitTime)
- {
- yield return new WaitForSeconds(waitTime);
- print("WaitAndPrint:" + Time.time);
- }</span>
因为start为顶级函数,所以会阻塞在这里,直到StartCoroutine(WaitAndPrint(2.0F))执行完毕,输出为:
starting:0
WaitAndPrint:2.00315
done:2.00315
<pre name="code" class="csharp"><pre name="code" class="csharp"><pre name="code" class="csharp"><p></p><pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> <pre></pre> </pre></pre></pre>
0 0
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D之协程(Coroutines & Yield )
- Unity3D开发之协程(Coroutines & Yield)不执行的原因
- unity3D Overview: Coroutines & Yield 协同程序 & 中断
- Coroutines & Yield 协同的理解
- Unity3D中yield协程的使用
- Unity - coroutines 协程揭秘
- Unity3D coroutines in detail
- unity3d 协程Coroutines
- C#笔记 协程coroutines yield return
- Unity里的协程Coroutines
- JAVA日期
- Spark学习笔记之-Spark-cassandra-connector阅读
- (1)AAABBBBCC–>A3B4C2
- HDU 3535 AreYouBusy(混合背包与分组背包)
- java垃圾回收机制和内存泄露
- Unity3D之协程(Coroutines & Yield )
- JAVA读写文件
- 谈测试面试(下)
- 在iBooks store 发布Epub格式电子书
- 快速排序
- js二级菜单的制作(有动画)
- Spline(三次样条插值)
- 2015.9.9
- 高效程序员的狂暴之路