协同程序js版,Coroutines in Unity3d (Javascript version)
I’ve been working with Unity3d for some time now and I couldn’t really make good use of the coroutines because I was unable to truly understand them. After some time playing with them and making some experiments I realized their true power. I couldn’t believe what I was missing! So, a couple of fellow game devs asked me to explain the concept to them. I decided that a blog post was the perfect way to do it and, at the same time, share this with everyone.
Coroutines: Special type of functions(in the programming sense) that allows to stop it’s own execution until certain condition is met.
1 function MyCoroutine()
2 {
3 DoSomething():
4 yield; //Mystery here
5 DoSomethingElse();
6 }
When you invoke this function (start the coroutine) it will behave just like any other normal function you have ever seen, until it reaches the yield instruction. The yield instruction explained: The yield instruction works like a return statement in the sense that it stops the execution of the function and returns control to the code that invoked that function. The main difference is that the yield instruction lets you delay the execution of the code that is after it (in the last example, the DoSomethingElse() statement).
01 function MyCoroutine()
02 {
03 DoSomething(): //Do this immediately
04 yield; //Return control to the caller
05 DoSomethingElse(); //This will be executed one frame later
06 }
07
08 void Start()
09 {
10 MyCoroutine();
11 }
What happens if you have more code after the MyCoroutine call? Let’s see an example with some prints:
01 function MyCoroutine()
02 {
03 print("This is printed second");
04 yield; //Return control to the Start function
05 print("This is printed one fourth, exactly one frame after the third");
06 }
07
08 void Start()
09 {
10 print("This is printed first");
11 MyCoroutine();
12 print("This is printed third");
13 }
You can control when do the code after the yield instruction will be executed. It depends on the parameter of the yield instruction, according to the following table
Nothing: It will wait one frame
Another coroutine invocation: It will wait until the invoked coroutine finishes execution
A WaitForSeconds object: It will wait certain amount of time
Confused? Here are the examples:
01 function MyCoroutine()
02 {
03 DoSomething(): //Do this immediately
04 yield WaitForSeconds(2); //Return control to the caller
05 DoSomethingElse(); //This will be executed 2 seconds after
06 }
07
08 void Start()
09 {
10 MyCoroutine();
11 }
01 function MyCoroutine()
02 {
03 DoSomething(): //Do this immediately
04 yield MyOtherCoroutine(); //Go and execute MyOtherCoroutine!
05 DoSomethingElse(); //This will be executed after MyOtherCoroutine finished execution
06 }
07
08 function MyOtherCoroutine()
09 {
10 DoStuff(): //Do this immediately
11 yield WaitForSeconds(2); //Return control to the caller (in this case the Start function)
12 DoMoreStuff(); //This will be executed 2 seconds after
13 //MyOtherCoroutine finishes execution here
14 }
15
16 void Start()
17 {
18 MyCoroutine();
19 }
As you can see, coroutines are very powerful and easy to use once you understand how they work. I will post some usage examples and the C# version of the scripts on this post soon.

very good, thanks