The Coroutine in unity is achieved through yield expression; This code is seen everywhere in the official script.
Questions:
What is yield?
What is Coroutine?
What's so strange about Unity's coroutine program execution process?
What is the principle of coroutine in unity and how is it implemented?
What issues do you need to be aware of when using unity Coroutine?
One, yield of the program execution characteristics in several languages :
The yield in Lua is to suspend and pass parameters to the resume, which is the function of the cooperative functions. Resume enables the cooperative function to hang and run and pass parameters to the synergistic function.
The yield return/break in C # is used to return the values (like iterations) inside the function query collection generator and record the current scene, and continue execution from the yield field of the last record at the next query, until the execution continues without, then exit this yield logic. Yield break terminates the yield iteration logic and jumps out.
Yieldimplementation:
1). Caller callsfunction
2). Caller Requestsitem Request an element on demand
3). Next itemreturned returns the requested element
4). Goto Step #2
In Python, yield expression, the function of yield has become a generator, call the function to return an iterator object, call the next method with an iterator object (or automatically call the next method in the loop), start executing the function, execute to yield Expression breaks, returns the current value of the iterator, preserves the scene, and the next call to next is executed from the field, and the iteration is finished. You can see that yield in Python is similar to yield in C #, to create a generator, to break the value of an iterator when executing, and to record the scene and continue the execution next time from the field.
The yield in unity is similar to that in C#,python, because unity is based on the. NET Framework, and the unity script begins with Boo (a variant of Python). It's just that unity has more coroutine attribute types, and Startcoroutine's coroutine management classes. Instead of starting a new thread, Startcoroutine starts a synergistic program, and the default unity all code is in one line approached (http://answers.unity3d.com/questions/280597/ new-thread-vs-startcoroutine.html).
The principle of Coroutine language level:
Two years ago, the process seemed to be a very advanced thing, and then most languages supported the process more or less. I am more familiar with the Python Gevent,lua of Coroutine,go's goroutine. In particular, LUA and go, the language itself supports the co-process. The co-process is also called lightweight threading. Popular point is to define a lot of tasks, and then through a thread round each task is executed, collaborative operation. What makes it so powerful is that every time it runs to a task, it can start running from where the task was last interrupted. In our general impression, only the operating system to the thread scheduling when it will do such things, to carry out a variety of stacks, save state. And the co-process, a total of just running in a thread, if you use the thread itself, the system stack, long time. So here, the implementation of the time is to use memory to simulate the operation of the stack. Concrete implementation, I think the complexity must be very small.
We know that threads are lighter than processes, so a thread consumes less resources than processes, and context switching is more economical than process. The turndown thread is more lightweight and context switching is faster. So in the server programming aspect gives the person infinite imagination. Although there is no mainstream Web server to adopt the process. But the performance of the Web services developed by the go language has come to the fore.
second, Unity's coroutine implementation phenomenon :
The first method:
Voidstart () {print ("starting"+time.time); Startcoroutine (Waitandprint (2)); Print (" Done"+time.time); } IEnumerator Waitandprint (floatwaitTime) { yield return Newwaitforseconds (waitTime); Print ("Waitandprint"+time.time); }
The sequence of execution of this code is 12435
Executes to 4 the event is registered, the control is given to the external thread, the external thread executes to 3, the event occurs, and the execution mechanism of the program is goto to the process where the stack information is executed 5 statements.
//Use this for initialization voidStart () {Startcoroutine (Ienumeratorstart ()); } //Update is called once per frame voidUpdate () {} IEnumerator Ienumeratorstart () {print ("starting"+time.time); yield returnStartcoroutine (Waitandprint (2.0F)); Print (" Done"+time.time); } IEnumerator Waitandprint (floatwaitTime) { yield return Newwaitforseconds (waitTime); Print ("Waitandprint"+time.time); }
The sequence of execution of this code is 12453
Why? Such a strange way of doing it.
The program executes to 4, executes the yield return expression to register the event to surrender control to the outside, because the outside also needs to hand over control also need to execute the expression statements after yield return so it will re-enter the Waitandprint function then the current state of the next step execution, so execute to 5, Yield return after the expression statement execution complete control is fully surrendered, before executing 3, the root cause is that yield return cannot be directly nested behind need to follow an expression (event).
V. Issues to be aware of when using coroutine in unity:
1. Place of use and place of inability to use:
Yield coroutine must be called in Monobehaviour or classes that inherit from Monobehaviour. Yield cannot be used in update or fixedupdate.
2. Open the co-process:
Startcoroutine (String methodName) and Startcoroutine (Ienumeratorroutine) can open a co-process,
Difference:
When using a string as a parameter, only one parameter can be passed at most when the process is turned on, and the performance consumption will be greater. The use of IEnumerator as a parameter does not have this limitation.
3. Delete the process:
1). In Unity3d, use Stopcoroutine (stringmethodname) to terminate the Monobehaviour a cooperative program that specifies the method name, using the Stopallcoroutines () To terminate all of the monobehaviour that can be terminated by this agent.
Including Startcoroutine (IEnumerator routine).
2). There is also a way to terminate the cooperative program, the active property of the Gameobject is set to False, when the active is set to Ture again, the co-program will not be opened;
This does not take effect if you set the enabled script for the collaboration program to false.
Differences in use between 4.js and C #:
Yield return instead of yield is used in C #.
The yield (break) statement in C # must be in the IEnumerator type, the return type of the C # method is IEnumerator, and the return value such as (Eg:yield return new Waitforseconds (2); or yield returnnull);
Coroutine in the Unity3d of "unity3d/c#"