Co-process: the cooperative program, while the main program is running, open another section of logic processing, to coordinate the execution of the current program.
Two ways to open a co-process
1, Startcoroutine (string methodName)
Attention:
(1), parameter is the method name (string type), this method can contain a parameter
(2), formal parameter method can have return value
2, Startcoroutine (IEnumerator method)
Attention:
(1), parameter is the method name (TestMethod ()), the method can contain more than one parameter
(2), the Ienumrator type method cannot contain a ref or out type parameter, but can contain the passed reference
(3), must have a return value, and the return value type is Ienumrator, the return value is used (yield Retuen + expression or value, or yield break) statement
There are two ways to terminate the co-process:
Stopcoroutine (String methodName), can only terminate the specified co-process
Note When using:
Calling the Stopcoroutine () method in a program can only terminate a thread that starts as a string
Stopallcoroutine (), terminate all co-processes
yield: Pending, when the program encounters the yield keyword, it hangs, pauses execution, and resumes execution from its current position when the condition is met
Yield return 0 or yield return NULL: The program continues execution from its current position in the next frame
Yield return,......: program awaits ... Continue execution from current position after frame
Yield return new Waitforseconds (n): Program waits n seconds to continue execution from its current position
Yield New Waitforendofframe (): Resumes execution from the current location after all rendering and GUI program execution is complete
Yield New Waitforfixedupdate (): All Fixedupdate () functions in the script are executed and continue from the current position
Yield return WWW: Wait for a network request to complete and continue from its current location
Yield return Startcoroutine (): Waits for the execution of a coprocessor to resume from its current position after completion
Yield break
If the yield break statement is used, it will cause the execution condition of the coprocessor not to be satisfied, not to continue executing the program from the current position, but to jump out of the function body directly from the current position and back to the root of the function.
The principle of execution of the co-process
The return value of the IEnumerator function, which is an iterator that can be used as a pointer to a node executing a sequence, provides two important interfaces, namely current (which returns the currently pointed element) and MoveNext () (moving the pointer back one unit, True if the move is successful)
The yield keyword is used to declare the next value in a sequence or a meaningless value, and if yield return x (x refers to a specific object or value) is used, then MoveNext returns True and current is assigned to X, if yield is used Break causes MoveNext () to return false
If the MoveNext function returns true means that the execution condition of the coprocessor is met, it can continue execution from its current position. Otherwise, you cannot continue from the current location.
The use of the C # coprocessor.