Unity Coroutine Detailed (b)

 

• Introduction
• Part 1. synchronization wait
• Part 2. Asynchronous coroutine
• Part 3. Synchronization coroutine
• Part 4. Parallel coroutine

 

1, introduce
  each Unity script has two important functions: beginning and updates. The role of the former is when an object is created, the latter call in every frame. Design requirements, the next frame update ended only to begin. In this way there will be a design limitation: the duration is not easy to update more than one.
Frankly, each custom behavior you can think of can be achieved with a start and updates. However, based on the occurrence of an event of multiple frames to be difficult to achieve some (such as animation, dialogue, waiting ...). This is because it can not write to set a continuous stream segment must, in many distribution frame. This tends to make the code difficult to write, it is very difficult to maintain.
If you can without any constraints in the short single frame it is very perfect. If you are a programmer, you must know the concept of threads. A thread is a code segment executed in parallel, using threads need to be cautious. This is because when multiple threads without limitation, a shared variable problems. Unity of design does not recommend the use of threads. However, Unity provides a compromise solution: coroutine. Coroutine continued more than one time. In addition, the coroutine execution can be interrupted and resumed in any case.
Coroutine is a conventional C # function that returns a IEnumerator. To coroutine execution (and different from the previous function), a method must be used StartCoroutine (UnityDoc). E.g:

void Start ()
{
    // Execute A as a coroutine
    StartCoroutine( A() );
}
  
IEnumerator A ()
{
    ...
}

A as the coroutine execution. StartCoroutine method terminates immediately, while generating new coroutine parallel.

 

2, synchronization wait

  If you've used coroutine, you should have met WaitForSeconds (float / int). Like other classes inherit YieldInstruction, which allows coroutines a brief pause. When connected yield, WaitForSeconds provides a way to delay the execution of the remaining code.
The following code shows how to use the coroutine:

IEnumerator A()
{
    ...
  
    yield return new WaitForSeconds(10f);
  
    ...
}

UML sequence diagram above (Wikipedia), to verify the effect of WaitForSeconds. When invoked coroutine (i.e., call A), it is suspended until a certain time consuming. This waiting is called synchronous because coroutine wait for the completion of another operation.

 

3, asynchronous coroutine

  Unity also allows to open a new coroutine in existing coroutine. The easiest way is to use StartCoroutine. So call it, newborn coroutine will coexist with previous coroutine. They direct interaction does not occur, the most important thing is that they do not wait for each other. Compared with the previous synchronization wait, this is asynchronous, two coroutine Do not try to keep pace.

IEnumerator A()
{
    ...
  
    // Starts B as a coroutine, and continue the execution
    StartCoroutine( B() );
  
    ...
}

Note that, in this case B is a completely separate coroutine. Termination will not affect B, and vice versa.

 

4, synchronize coroutine

  You may perform nested coroutine and wait for it to implement complete. The easiest way is to use yield return.

IEnumerator A()
{
    ...
  
    // Waits for B to terminate
    yield return StartCoroutine( B() );
  
    ...
}

Notably, since the A suspended during execution of B, in this particular case it does not need to start another coroutine. Some might like this trying to optimize coroutine:

IEnumerator A()
{
    ...
  
    // Executes B as part of A
    B();
  
    ...
}

Execution of an ordinary function B has the same effect, except that B is performed within a single frame. In contrast, by using StartCoroutine, A while the next frame has been tentatively can begin.
This example is provided to introduce the introduction of a more complex example, the synchronization coroutine.

 

5, parallel coroutine

   When activated by coroutine StartCoroutine, it returns a special object. This can be used to query the status of coroutine, at any time waiting for it to end.
The following examples coroutine B is executed asynchronously. A parent class B may continue until such time as needed. If necessary, it can wait a reference for synchronization of concessions in B. 

IEnumerator A()
{
    ...
     
    // Starts B as a coroutine and continues the execution
    Coroutine b = StartCoroutine( B() );
     
    ...
     
    // Waits for B to terminate
    yield return b;
     
    ...
}

 

If you want to start several concurrent coroutines, then it would be very useful, all code running in the same moment: 

IEnumerator A()
{
    ...
     
    // Starts B, C, and D as coroutines and continues the execution
    Coroutine b = StartCoroutine( B() );
    Coroutine c = StartCoroutine( C() );
    Coroutine d = StartCoroutine( D() );
     
    ...
     
    // Waits for B, C and D to terminate
    yield return b;
    yield return c;
    yield return d;
     
    ...
}

This new model allows any number of parallel computing, resume execution when the parallel data is completed.

Guess you like

Origin www.cnblogs.com/unity3ds/p/10993512.html