Unity process in-depth parsing and principle

Source: Internet
Author: User

First, just put a piece of code.

1 usingSystem.Collections;2 usingSystem.Collections.Generic;3 usingUnityengine;4 5  Public classCoroutinetest:monobehaviour6 {7 8     voidStart ()9     {TenDebug.Log ("Start Begin"); One  ACustomcoroutine = Delayprint ("Hi"); -  -Debug.Log ("Start End"); the     } -  -     BOOLIsstartcoroutine =true; -IEnumerator Customcoroutine =NULL; +     voidUpdate () -     { +Debug.Log ("Section"+time.framecount+"Update is executing"); A         if(Isstartcoroutine) at         { -             if(Customcoroutine.movenext ()) -             { - Debug.Log (CustomCoroutine.Current.ToString ()); -             } -         } inDebug.Log ("Section"+ Time.framecount +"end of update execution"); -     } to  +      PublicIEnumerator Delayprint (stringsomevalue) -     { theDebug.Log ("Delayprint Start"); *         yield return 0; $Debug.Log ("delayprint Debug:"+somevalue);Panax NotoginsengDebug.Log ("Delayprint End"); -     } the  +}
coroutinetest

The code results are as follows:

Is this a look that is basically the same as the startcoroutine we normally use in unity? There are still different places, because of the special processing (guessing) of their own process management within unity.

The sequence diagram for the co-location within unity is as follows:

And the one we wrote on it will only run in the update, because we write MoveNext () in the update, and if we want to run our co-operation somewhere else, we can write it somewhere else.

So the essence of the unity process is the polling mechanism inside unity and the IEnumerator iterative Save Context execution code snippet.

And we usually in the co-process method yield return xxx just put the control of this time code run back to the main thread (similar to the operating system hangs), other threads (similar to the WWW class, which will really open another thread to execute, otherwise essentially single-threaded), The state of the current method code snippet is then saved, and the state of the Code in Unity will continue to execute when it is again polled back to suspended, until there is no yield return, which results in a piece of code execution.

Also when only yield return XXX when ienumerator.movenext () will be true, then ienumerator.current is yield return xxx of XXX.

So the yield return waitforseconds in unity returns to the internal coprocessor in unity for judgment processing, for example, if the return Waitforseconds class gives the process a time difference to delay processing.

If there is no yield return, that is, there is no return value, then Ienumerator.movenext () is false, which exits unity polling.

Probably this way, if more want to understand the details can Baidu IEnumerator and function and the above code to step through the debugging can.

Reference: https://www.zhihu.com/question/34878524/answer/61555725

https://www.zhihu.com/question/23895384/answer/26066323

http://dsqiu.iteye.com/blog/2029701

Coroutinemanager plug-in source code (this plugin is in its own Update method to constantly poll the Coroutinemanager collection, get the MoveNext () state of each association, so that some simple judgment to manage a co-process)

Unity coprocessor in-depth parsing and principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.