Unity3d provides a tool called the "co-process", which is called by using Startcoroutine () to add a method to invoke the method. The method that is called is as follows: The return value must be of type IEnumerator. So why use a co-process? Usually this is to cope with a certain kind of need, such as to delay the execution of a certain piece of code, or use WWW for some blocking operations such as request and load.
The co-process is not related to multithreading. Each frame of the coprocessor is executed, and the time period is after lateupdate. So it's just a function that executes every frame in the main thread. The principle used by the coprocessor is similar to the Foreach Loop, which is implemented using iterators, so it also includes the current property of the IEnumerator interface and the MoveNext () method. Both of these things are important to iterators.
The explanation for current on MSDN is: Gets the current element in the collection, the MoveNext () is interpreted as: advances the enumerator to the next element f O collection. As can be seen from these two interpretations, current returns a collection of existing elements, and MoveNext () pushes the loop down one element. The return value of MoveNext () is bool, and when True is returned, it goes down one level and returns false to stop. If you loop to the last element, you will definitely return false.
For the thread, when the yield return statement is made, check that the condition after yield return is satisfied and, if satisfied, executes the following code. If not, the position is recorded and the next frame continues to be checked until it is satisfied. This reflects the main role of the association, if you use the WWW to download a data, you definitely want to know when the download is complete. Only the next line of yield return www will be executed if the download is finished.
Let's look at a test class, which comes from the network and alternately executes the other two processes through a loop within a co-process. Note that these processes are all within the main thread and have the freedom to access Unity3d objects and components.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingUnityengine;usingSystem.Collections; [Requirecomponent (typeof(Guitext))] Public classHijack:monobehaviour {//This'll hold the counting up coroutineIEnumerator _countup; //This is the counting down coroutineIEnumerator _countdown; //The coroutine we are currently//HijackingIEnumerator _current; //A value that would be updated by the Coroutine//That's currently running intValue =0; voidStart () {//Create Our count up coroutine_countup =Countup (); //Create Our count down coroutine_countdown =countdown (); //Start Our own coroutine for the hijackStartcoroutine (Dohijack ()); } voidUpdate () {//Show The current value on the screenGuitext.text =value. ToString (); } voidOngui () {//Switch between the different functions if(Guilayout.button ("Switch Functions")) { if(_current = =_countup) _current=_countdown; Else_current=_countup; }} IEnumerator Dohijack () { while(true) { //Check If we have a current coroutine and MoveNext on it if we do if(_current! =NULL&&_current. MoveNext ()) {//Return Whatever the coroutine yielded, so we'll yield the//same thing yield return_current. Current; if(_current. current!=NULL) Debug.Log (_current. Current.tostring ());//wait for seconds OR null } Else //Otherwise wait for the next frame yield return NULL; }} IEnumerator Countup () {//We have a local increment so the routines//get independently faster depending on how//Long they has been active floatincrement =0; while(true) { //Exit If the Q button is pressed if(Input.getkey (KEYCODE.Q)) Break; Increment+=Time.deltatime; Value+=mathf.roundtoint (increment); yield return NULL; }} IEnumerator Countdown () {floatincrement =0f; while(true) { if(Input.getkey (KEYCODE.Q)) Break; Increment+=Time.deltatime; Value-=mathf.roundtoint (increment); //This coroutine returns a yield instruction yield return NewWaitforseconds (0.1f); } }}
Now simply explain the class. First, a variable of three IEnumerator interface types is defined to accept the return value of the coprocessor, and one of the Dohijack () threads is called in the Start method. This process has an infinite loop inside the loop to check if the _current is assigned, and if there is a value, call the MoveNext () method inside the IEnumerator interface, which is equivalent to executing the _current corresponding method for Each loop. As for _current. Current returns the return value of two Countup and countdown, outputting a value of NULL or waitforseconds (), exactly the return value followed by yield return.
Unity3d the process of the association