Coroutines in Unity3d (C# version)----在unity3D中的协同(基于C#)

Coroutines in Unity3d (C# version)—-在unity3D中的协同(基于C#)

分类: Unity

2013-07-07 23:10 1022人阅读 评论(0) 收藏 举报

原文链接:http://www.blog.silentkraken.com/2010/01/23/coroutines-in-unity3d-c-version/

Coroutines in C# work the same way that they do in Javascript (UnityScript), the only difference is that they require more typing (they have a slightly more complicated syntax). You should see the blog post on Javascript coroutines first.
Here, I present the differences:

协同在C#的编程中与在Javascript (UnityScript)的编程中工作原理是一样的,唯一不同是它需要输入更多的代码(需要更加复杂的语法)。你也应该去看看 blog post on Javascript coroutines first这篇文章。

在这里,我介绍一下他们的差异:
Coroutines must have the IEnumerator return type:
需要协同的程序必须是IEnumerator的返回类型:

[java] view plaincopy

1. IEnumerator MyCoroutine()
2. {
3. //This is a coroutine
4. }

To invoke a coroutine you must do it using the StartCoroutine method:
你必须使用StartCoroutine方法来调用协同

[java] view plaincopy

1. public class MyScript : MonoBehaviour
2. {
3. void Start()
4. {
5. StartCoroutine(MyCoroutine());
6. }
7. IEnumerator MyCoroutine()
8. {
9. //This is a coroutine
10. }
11. }

The yield statement becomes yield return in C# :
在C#中yield语句变成yield return

[java] view plaincopy

1. IEnumerator MyCoroutine()
2. {
3. DoSomething();
4. yield return 0; //Wait one frame, the 0 here is only because we need to return an IEnumerable
5. DoSomethingElse();
6. }

Remember that we need to return an IEnumerable, so the Javascript yield; becomes yield return 0; in C#

记住,我们需要返回一个IEnumerable类型,Javascript中yield在C#中要变成yield return 0;

Also, since C# requires you to use the new operator to create objects, if you want to useWaitForSeconds you have to use it like this:

在C#中,如果你要使用WaitForSeconds,你必须通过new 关键字创建一个object

[java] view plaincopy

1. IEnumerator MyCoroutine()
2. {
3. DoSomething();
4. yield return new WaitForSeconds(2.0f);  //Wait 2 seconds
5. DoSomethingElse();
6. }

Happy coroutining。
尽情去协同吧。

PS:如翻译有误,请指出,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值