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

来源:互联网 发布:单页面淘宝客 编辑:程序博客网 时间:2024/05/21 10:57

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 coroutine4. }

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

[java] view plaincopy

1. public class MyScript : MonoBehaviour2. {3. void Start()4. {5. StartCoroutine(MyCoroutine());6. }7. IEnumerator MyCoroutine()8. {9. //This is a coroutine10. }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 IEnumerable5. 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 seconds5. DoSomethingElse();6. }

Happy coroutining。
尽情去协同吧。

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

0 0