Talking about the Unity3d process

Source: Internet
Author: User

Co-process

Understanding: The process is not a thread, nor does it execute asynchronously (knowing the line).

1. The monobehaviour and the update function are also executed in mainthread (it is important to understand the meaning of this statement).

void Start () {        startcoroutine (Hellocoroutine ());} void Update () {        Debug.Log ("Update ...");    void Lateupdate ()    {        Debug.Log ("Lateupdate ...");    IEnumerator Hellocoroutine ()    {        while (true)        {            Debug.Log ("Coroutine ...");            yield return null;        }    }


Compare the above code with two sheets. This way of writing is like an advanced update. At least it should be seen that this process of writing can complete the function of update.
2. Not the same place as the update.
IEnumerator Count ()    {        int seconds = 0;        while (true)        {for            (float timer = 0; timer < 2; timer + = Time.deltatime                ) yield return 0;            seconds++;            Debug.Log (seconds + "seconds have passed since the coroutine started.");        }    }

  

3.yield

yiled return null equals yield return 0

My understanding here is to stop the executing method and start execution from the next frame (typically 0.02 seconds, as is the case with each frame of the update, depending on the timer set by unity).

4. The co-process is capable of passing parameters.

5. The co-process can also be nested.

IEnumerator hellocoroutinue ()    {        Debug.Log ("Start----");        Yield return Startcoroutine (Wait (0.2f)); Yield return new waitforseconds (0.2f); The result is the same.        Debug.Log ("End----");}    IEnumerator Wait (float s)    {for        (float timer =0;timer< s;timer+=time.deltatime)        {            Debug.Log (" Current Timer "+ timer. ToString ());            Yield return 0; yield return null;        }        Debug.Log ("Wait ...");    

  

Fancy draw line time difference, again verified with update like. The time to pause is the same.

You can see the method that paused the current method to execute yield return.

Additional notes:

A. Multiple processes can be run concurrently, and they will be updated according to their respective boot order;

B. If you want multiple scripts to access a co-process, you can define it as a static co-process;

(this blog is a reference to a blog on the web, simplifying itself, adding a little bit of their own understanding)

Talking about the Unity3d process

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.