Introduction and use of Unity3d process

Source: Internet
Author: User
Tags unity 5

I'm a happy porter, http://blog.csdn.net/u011397120/article/details/61236055.

----------------------------------------------------------------------Dividing line xx----------------------------------- ---------------------------------------

This article is an individual's understanding and summary of the unity process. The Unity coprocessor is a bit like a thread, but not a thread. Because the process is still executed in the main thread, there is no need to consider the problem of synchronization and locking when it is used. The process simply controls the code until a specific time and then performs the next steps.

Start the process

The Startcoroutine method is used in Unity 5.x to open the process in several ways.

Form one startcoroutine (Customcorutinefn ());  Startcoroutine (Customcorutinefn (7));//Pass parameters to the method//form two startcoroutine ("Customcorutinefn"); Startcoroutine ("Customcorutinefn", 7);//passing parameters to the method

Both of these forms can open the current Monobehaviour object, Note: When you want to stop a Monobehaviour object in the process, the opening and stopping of the process need to use the same form, not mixed use .
There are several wait methods in the process, for example: Wait until the fixedupdate is finished, and the code is as follows.

    bool Canexcute = true;    void Fixedupdate ()    {        if (Canexcute)        {            Debug.Log ("Fixedupdate");        }    }    IEnumerator corutine_waitforfixedupdate ()    {        yield return new waitforfixedupdate ();        Debug.Log (String. Format ("====>{0}    Time:{1}", 1, time. time));        Yield return new waitforfixedupdate ();        Debug.Log (String. Format ("====>{0}    Time:{1}", 2, Time));    }

The output results are as follows.

The Official document Monobehaviour's function execution sequence diagram is a good description of the timing of the execution of the co-process.

Above is just one, please see the official documentation for more information.
Link https://docs.unity3d.com/Manual/ExecutionOrder.html

Yield null: The coprocessor will continue execution after the update of all scripts in the next frame. Yield Waitforseconds: The process is deferred for a specified time, and all the script's update of the current frame is completed before the execution continues. Yield waitforfixedupdate: The process continues after the fixedupdate of all scripts is executed. Yield www: After the www download resource is completed, the process will continue to execute. Yield startcoroutine: After the execution of the specified coprocessor is completed, the process continues. Waitforsecondsrealtime: Similar to waitforseconds, but not affected by time scaling. Waitwhile: The next step is performed when the return condition is false.

The execution of the process will also be affected by other factors, such as: When the time scale value Time.timescale is modified, zoom in or zoom out. The Fixedupdate method is affected, and the waitforfixedupdate is also affected, and the time specified by Waitforseconds may be inconsistent with the actual time when the larger object is loaded synchronously in the Update method. So when executing a co-wait , depending on the situation.

What is the execution order of multiple Gameobject objects opening the process?
If there is a A, B, two Gameobject object in the scene, all are waiting with waitforfixedupdate, then the part that waits for execution will be executed at the end of the fixedupdate of A, B, two object before the next executable part of the current frame begins. The source code is as follows:

    void Awake ()    {        startcoroutine (corutine_waitforfixedupdate ());    }    IEnumerator corutine_waitforfixedupdate ()    {        Debug.Log (string. Format ("A: {0}", 0));        Yield return new waitforfixedupdate ();        Debug.Log (String. Format ("A: {0}", 1));    }    BOOL Canexcute = false;    void Fixedupdate ()    {        if (!canexcute)        {            Canexcute = true;            Debug.Log ("A fixedupdate");        }    }

after execution , the output is as follows.

Stop the co-process

Multiple processes may be turned on in development, and you can use Stopcoroutine if you want to stop one of them. But when you use it, you need to be aware that the way to stop the process is consistent with the way you open it. Stopcoroutine ("Customcorutinefn") must be used in pairs with startcoroutine ("Customcorutinefn"), with Startcoroutine (Customcorutinefn ()) Used together is completely invalid .
There are two ways to stop a co-process by Stopcoroutine the overloaded method. In this case, the following:

    IEnumerator cor;    void Awake ()    {        //Note save IEnumerator variable.        Cor = Corutine_waitforfixedupdate ();        Startcoroutine (COR);        Startcoroutine (Corutine_stop ());    }    IEnumerator corutine_waitforfixedupdate ()    {        Debug.Log (string. Format ("A: {0}", 0));        Yield return new Waitforendofframe ();        Debug.Log (String. Format ("A: {0}", 1));    }    IEnumerator corutine_stop ()    {        yield return new waitforfixedupdate ();        Stop the process via COR        //rather than this. Stopcoroutine (Corutine_waitforfixedupdate ());        This. Stopcoroutine (COR);    }

If you want to stop multiple threads, you can use the Stopallcoroutines method, but this method only stops all the threads in the current Monobehaviour class instance. Others are unaffected.
If you want to stop the Gameobject in all the script components on the Gameobject, disabling the script component does not stop the co-process, just disable it.

How to use the output of the Coprocessor 10 group count (5 times per group, 1 seconds each)?
The source code is as follows:

Using system.collections;using Unityengine;public class mcorutine:monobehaviour{public    float time = 1;    void Awake ()    {        startcoroutine (Firstlayercorutine ());    }    IEnumerator Firstlayercorutine ()    {for        (int i = 0; i <; i++)        {            Debug.Log (string. Format ("{0} Group", I + 1));            Yield return Startcoroutine (Secondlayercorutine ());        }    }    IEnumerator Secondlayercorutine ()    {for        (int i = 0; i < 5; i++)        {            Debug.Log (string. Format ("{0}", i + 1));            Yield return new waitforseconds (time);}}    

There is a lot to note about the use of the process, and here is a link to monitor and optimize the runtime of the co-operation.
Http://gulu-dev.com/post/perf_assist/2016-12-20-unity-coroutine-optimizing

Introduction and use of 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.