I. The concept and nature of the Coroutine (co-process) 
In some of the information on the Internet, Coroutine has been described as a thread, which is inaccurate. Because Coroutine is not a new thread, it is still part of the main thread. Coroutine is essentially a lightweight thread, and it costs a lot less than using thread. Multiple coroutine can be executed in the order of a thread, a coroutine if in block state, you can hand over the execution, let the other coroutine continue to execute.
 
 
In Unity, Coroutine is executed after all the update functions have been performed, that is, after lateupdate (), primarily to assist the main thread in working, as if a separate update () function was manually added, In general, we use the function of delay triggering. The following is a sequence diagram of the execution of Monobehavior, and you can see that coroutine is behind Lateupdate.
 
 
 
And we notice that in the image above, there are several yield modules on the left side of the coroutine, and this is how we will use the content in Coroutine below.
 
second, the use of Coroutine in the untiy inside, if you want to use a function fun as a coroutine, there are two points to be aware of the 1, the return type is to IEnumerator 2, the use of yield
Yield, in the coroutine is more like a traffic light function, before it satisfies the conditions immediately behind it, the process will hang, the execution of the call to its parent function, meet the conditions can be executed yield the following code. You can execute the following code to see the effect
 
Using Unityengine;
Using System.Collections;
public class Exampleclass:monobehaviour
{
	IEnumerator waitandprint ()
	{
		//suspend execution for 5 Seconds
		yield return new waitforseconds (5);
		Print ("Waitandprint" + time.time);
	}
	IEnumerator Start ()
	{
		print ("starting" + time.time);
		Start function Waitandprint as a coroutine
		yield return startcoroutine ("Waitandprint");
		Print ("Done" + time.time);
	}
}
Everyone should be able to get the following output (my Unity version is 5.0.1F1) 
 
In addition, you can execute the following code, you can look at the similarities and differences with the above 
Using Unityengine;
Using System.Collections;
public class Exampleclass:monobehaviour
{
    IEnumerator waitandprint ()
    {
        //suspend execution for 5 Seconds
        yield return new waitforseconds (5);
        Print ("Waitandprint" + time.time);
    }
    void Start ()
    {
        print ("starting" + time.time);
        Start function Waitandprint as a coroutine
        startcoroutine ("Waitandprint");
        Print ("Done" + time.time);
    }
}
The resulting output is as follows: 
 
As can be seen from the above, we are using Starcoroutine method to start a new coroutine, in the first code, the Start method is also declared as a IEnumerator type, when executed to the yield module, will be blocked, Waiting for the yield module to finish and then continue to execute, and when the new Coroutine is launched, when it starts the next time, it will not block the entire start, but instead the current execution position is recorded, and then the execution in the parent function is returned. After each frame, the yield is detected to be satisfied, and when the yield condition is met, it is returned back to yield execution. 
In Unity3d, a thread can be opened using Startcoroutine (String methodName) and Startcoroutine (IEnumerator routine). The difference is that using a string as a parameter allows the thread to be opened and terminated before the thread ends, whereas using IEnumerator as a parameter can only wait for the end of the thread to terminate at any time (unless the Stopallcoroutines () method is used), and when the string is used as a parameter. Only one parameter can be passed at most when the thread is turned on, and the performance consumption is a bit larger, while using IEnumerator as a parameter does not have this limitation. 
 
In Unity3d, use Stopcoroutine (string methodName) to terminate a synergistic program, using Stopallcoroutines () to terminate all the cooperating programs that can be terminated. However, both methods can only terminate the Monobehaviour in this program.
 
There is also a way to terminate the Coroutine by setting the active property of Gameobject to false, but setting it back to true does not start coroutine.
 
Note: Setting the script for coroutine to enable = False does not stop the coroutine because it is at the same level as Monobehavior.