About the use of the co-process, multi-threading, www network class in Unity

Source: Internet
Author: User

Co-process

We want to download a picture, load a resource, this time must not be loaded at once, or we do not have to wait for it to download the other operations, if so I stuck in the download picture that place, silly Live. We hope that as soon as we get the command to start loading, The main thread can go On.

We can start a co-process to download, and the main processes are still running. A bit like multi-threading, the difference is, actually it is in a thread, so we are in the main thread to open a logic to deal with the execution of the current program, or in the main thread.

1: in the main process to open another piece of logic processing, to coordinate the execution of the current program, but unlike multithreading is carried out in the main thread, and the original process is still going on,

2: through the Startcoroutine method to start a process, startcoroutine is a monobehaviour method, the method can start a co-process, each process has an entry function, the co-process must be a IEnumerator As a method of return value (entry function);

3: the Synergistic program can use the yield keyword to interrupt the collaborative program;

4: The co-process can also start a co-process;

5:waitforseconds (): wait for how long after the interruption of the process;

Example of a co-process

1. Create a Unity project and file directory to save the scene

2. Create an empty node game, and then create a script game to mount under the Gamej node

Open the game script

usingsystem.collections;usingSystem.Collections.Generic;usingunityengine; public classGame:monobehaviour {Private intLevel=3; //Use this for initialization    voidStart () {//start a process that must be inherited from Monobehaviour to use         this. Startcoroutine ( this. Con_entry ()); //The main thread is still executing//...    }    //the co-process and the main thread are inside the same threads, and there will be no thread switching//the entry function of the co-processIEnumerator con_entry () {//code for the co-processDebug.Log ("con_entry run!!"); Debug.Log ("level :"+ this. level);//can also get the variable of this         this. Startcoroutine ( this. Con_other ());//and open a co-process con_other//End//using yield to interrupt the coprocessor is to stop it.        yield return NULL; //after the completion of the code, such as to go online to catch something, download pictures and so on, after the operation//End} IEnumerator con_other () {Debug.Log ("Con_other");  this. Startcoroutine ( this. Con_entry_params (3));//and then open a con_entry_params, and pass the parameters                yield return NULL; } IEnumerator Con_entry_params (intA) {Debug.Log ("con_entry_params:"+a); yield return NewWaitforseconds (3);//set 3 seconds before interrupting the processDebug.Log ("after waitforseconds Send");//logic to handle the end of the process    }    //Update is called once per frame    voidUpdate () {}}

Multithreading

1:using system.thread;

2: Create a thread: thread r = new Thread (callback); R.start (); Start the running thread;

3: thread back function void Run () {}

4: When multiple threads access the same data, a "conflict" occurs, requiring a thread-safe way to access it;

5: The thread lock is the time to access the public data, the first to obtain the lock, the Non-lock threads will be suspended, directing the lock is Released.

6:public static Object o = new Object (); Lock (o) {thread-safe Execution code};

7: thread hibernation: thread.sleep (unit ms);

8: to avoid deadlocks between multiple threads;

About the use of the co-process, multi-threading, www network class in Unity

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.