查看: 2857|回复: 0
打印 上一主题 下一主题

[脚本章节]Overview: Coroutines & Yield 协同程序 & 中断

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-11 10:27:35 |只看该作者 |倒序浏览
When writing game code, one often ends up needing to script a sequence of events. This could result in code like the following.
写游戏代码,往往最终需要代码为连续的事件.结果会像这样:


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {private int state = 0;void Update() {if (state == 0) {state = 1;return;}if (state == 1) {state = 2;return;}}}


private var state = 0;function Update() {if (state == 0) {// do step 0  //做步骤0state = 1;return;}if (state == 1) {// do step 1 // 做步骤1state = 2;return;}// ...}


It is often more convenient to use the yield statement. The yield statement is a special kind of return, that ensures that the function will continue from the line after the yield statement next time it is called.
往往使用中断语句更为方便.中断语句是一个特殊的返回类型,它确保函数从中断语句的下一行继续执行.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {IEnumerator Awake() {while (***e) {yield return null;yield return null;}}}


while(***e) {// do step 0// 做步骤0yield; // wait for one frame // do step 1// 等待一帧// 做步骤1yield; // wait for one frame// 等待一帧// ...}


You can also pass special values to the yield statement to delay the***cution of the Update function until a certain event has occurred.
你也可以传递时间值到中断语句,Update函数会在中断时间结束后执行下一语句.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {IEnumerator Awake() {yield return new WaitForSeconds(5.0F);}}


// do somethingyield WaitForSeconds (5.0); // wait for 5 seconds等待5秒// do something more...


You can both stack and chain coroutines.
你能叠加和连接协同程序.
This example will***cute Do but continue after calling do immediately.
这个例子将执行Do,但是do函数之后的print指令会立刻执行.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {IEnumerator Do() {print("Do now");yield return new WaitForSeconds(2);print("Do 2 seconds later");}void Awake() {Do();print("This is printed immediately");}}


Do ();print ("This is printed immediately");function Do (){print("Do now");yield WaitForSeconds (2);print("Do 2 seconds later");}


This example will***cute Do and wait until it is finished before continuing its own***cution
这个例子将执行Do,并等待,直到Do完成再执行其他语句.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {IEnumerator Do() {print("Do now");yield return new WaitForSeconds(2);print("Do 2 seconds later");}IEnumerator Awake() {yield return StartCoroutine("Do");print("Also after 2 seconds");print("This is after the Do coroutine has finished***cution");}}


// chain the coroutine// 连接协同程序yield StartCoroutine("Do");print("Also after 2 seconds");print ("This is after the Do coroutine has finished***cution");function Do (){print("Do now");yield WaitForSeconds (2);print("Do 2 seconds later");}


Any event handler can also be a coroutine.

任何事件处理程序都可以是协同程序
Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can.
注意你不能在Update或FixedUpdate函数内使用中断,但是你能使用 StartCoroutine 开始一个函数.
查看 YieldIns***ction, WaitForSeconds, WaitForFixedUpdate, Coroutine and MonoBehaviour.StartCoroutine 可以获得更多使用中断的信息.



来源:unity3d圣典 更多分享尽在web3D纳金网http://www.narkii.com/



——————————————————————————


Subsections 章节

     [脚本章节]Scripting Overview 脚本概述
    [脚本章节]Overview: Script compilation 脚本编译(高级)
    [脚本章节]Overview: Performance Optimization 性能优化
    [脚本章节]Overview: The most important classes 重要的类
    [脚本章节]Overview : Writing Scripts in C# 使用C#书写脚本
    [脚本章节]Overview: Coroutines & Yield 协同程序 & 中断
    [脚本章节]Overview: Instantiate 实例
    [脚本章节]Overview: Member Variables & Global Variables 成员变量 & 全局变量
    [脚本章节]Overview: Vectors 向量
    [脚本章节]Overview: Accessing Other Game Objects 访问其他游戏物体
    [脚本章节]Overview: Accessing Other Components 访问其他组件
    [脚本章节]Overview: Keeping Track of Time 记录时间
    [脚本章节]Overview: Common Operations 常用操作
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 05:41 , Processed in 0.088503 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部