The Synergy program in Unity

Source: Internet
Author: User


Let's talk today about the coroutine of the collaborative process. (This article is knocked on the Internet bar, no unity, but all the conclusions have been run, whether you trust me or trust me, you have to run for a look, but also welcome error correction)
First say what is the Association process: The synergy is a very disgusting thing, its expression is very like a thread, the thread has contact with the friend may understand the meaning of my words, you have not touched the thread, then understand it will be some difficulty. However, it does not have a thread safety problem and can be used with confidence. This is not J. Irresponsible remarks white teeth with this guess, the fact is this: at the operating system level, that is, the older gods, think "concurrency" is a very fashionable thing, very good, so they allow the operating system to open the process. Later, they felt that in a process, if it can also be more good, then the thread came into being (these are some common sense that the code farmer should know: A system can run multiple processes, a process can be multiple threads concurrently). But since our unity is single-threaded, then there is a less familiar word: A thread can be multi-threaded. In this view, Coroutine can be said to be a lot of "concurrent" threads in the unity main thread. Why this "concurrency" is quoted, this will be an important part of our research.

Describes the definition of synergy, so you can look at how it will work in the project.
Here are some of the ways in which you might encounter some of the collaboration programs in the interview questions:

1. Do not use co-starting a function that returns an iterator.
void home to guests ()

Tea ();
Chatting with guests ();

IEnumerator tea ();

2. In the iterator function, yield return is a synergistic start. Such as

void home to guests ()

Startcoroutine (tea ());
Chatting with guests ();


IEnumerator tea ()
{
Yield return startcoroutine (make water ());
Find the tea can ();
}
IEnumerator make water ();
void Find tea can ();

3. In the iterator function, start a collaboration directly. Such as
void home to guests ()

Startcoroutine (tea ());
Chatting with guests ();

IEnumerator tea ()
{
Startcoroutine (water ());
Find the tea can ();
}
IEnumerator make water ();
void Find tea can ();

This is really not let the guys memorize or pretend to force, I wrote very fancy, is to let the guys do a knock to verify the HA!!!!
The following analysis of the next.
For the first article: this usage, the iterator "tea", is not at all concurrency, for example, in this case, although tea is an iterator function, but if you do so, it will be finished tea (thoroughly brewed tea) and then chat with the guests. If the tea is very time-consuming, then I am sorry, you will not chat with the guests during this period of time. The iterator here is in vain. Equivalent to a normal function.
For the second: after you have Startcoroutine, you start the concurrency immediately, that is, you are on the side of the tea, and start chatting with the guests. Now come in and see when you're making tea. In the synergy of the tea, you first yield return to do water. The meaning of this sentence is, "Wait for the water to finish", and then start looking for tea cans. It is worth noting that when you are making water and looking for a tea jar, you have already started chatting with the guests.
For the third article: once you have done the tea, chat with the guest again. When you make the tea, you are making water while looking for the tea jar.

For this example, look at it. There are a few gains:
1.yield return has nothing to do with return, yield return XXX translation adult words is "waiting for XXX to return", is a blocking operation of the cooperative program.
2. Open the Cooperative program, the implementation of pseudo-concurrency, although it looks like concurrency, but there are sequential execution order, so it is fundamentally different from the thread-threading theory, the order is unpredictable-unless you use the semaphore and so on to control the HA.
3. To open a synergistic program, it must be a function that returns an iteration. otherwise compiled. But the function that returns the iteration can not be called in the collaboration, so the compilation is passed, but basically does not write so, the code is not clean.

The main bone has been chewed, and the remaining one is when the Synergy program is completed?
1. Meet yield break--directly out of Ctrip, to some of the failure must jump out of the time, such as loading assetbundle time, the WWW has failed, behind the load bundle is not necessary, this time can yield break. This statement is very useful.
2. Execute to the last line-the last line does not have to be yield return xxx; I often end up with a excute delegate or something.
Add: Yield return null;yield return 0; no end-of-process Synergy!!!!

Understand these, to try to figure out, you can work together. These are the difficulties. Although it is disgusting, but it is not difficult, hope to help everyone!

The Synergy program 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.