Unity of in-depth understanding of coroutine

Recommended reading:

How to understand the syntax coroutine? Development in how to use? This is a problem for beginners Unity will face.

How to understand the coroutine syntax:

1. coroutine, by definition, "coroutine", to collaborate.
2. An operation is performed as in a game to wait one second. We can not let the game the main thread stuck and so one second, but open a coroutine collaborate, co-routines is also driven by the main thread (the thread so that it is not), will be in each frame to detect whether it has reached complete conditions. Performing an operation such that after one second, then when it is detected in one second thread emperor. Condition is satisfied, then it executes the previously set a good correlation operation.

Why should coroutine?

(1) Uniy core logic is single-threaded, in order to facilitate the development of the problem, unified lifecycle management, need not be considered a lock.
(2) some of the guide-step operation is very time-consuming, such as resource loading, if allowing users to use multi-threaded development, the entire logic of the development will be very complicated. Encapsulated by the coroutine, user-friendly. For the operation and some of the spent time, the engine will Unity through multi-threaded bottom to complete, and coroutine through the continuous state is determined to access the loading operation is completed.
Start coroutine approach:

method one:

StartCoroutine(IEnumerator routine)

Method Two:

StartCoroutine(string routine)StartCoroutine(string routine,object value) 
//通过传入函数名字的字符串启动协程的性能开销要更高,但这种方式启动的协程可以通过StopCoroutine(string methondname)来终止,这种方式还有一个限制就是只能传递一个参数。

Method coroutine termination
method:

StopCoroutine(string methondname)只能终止通过字符串名字启动的协程

Method Two:

StopCoroutine(Coroutine coroutine)

Method three:

StopAllCoroutines()终止所有协程

Method four: the termination can be used within coroutine

yeild break;

Another way is to direct the active object property set to false, there is a pit, easily lead bug

Some characteristics of the coroutine:
(1) can start another coroutine in a coroutine
(2) can suspend a coroutine another coroutine
(3) two Coroutine may work in parallel
(4) in the coroutine the implementation process can pause at any point in time by yield, the return value yeild determines when the coroutine resumes execution
(5) when operating in multiple game frame cycle, coroutines performance is very good, almost no additional performance overhead
( 6) StartCoroutine () function always returns immediately, no matter what you yeild the return value is
(7) at the end of the order of multiple coroutine is no guarantee that consistent with its start sequence, even if they are in the same frame among end

About yeild return value
(1) may be null, numbers, strings, Boolean even expressions, functions, nested coroutine.
(2) when the return is a function call, the assignment expressions, nested coroutine will directly call the function or expression.
(3) general coroutine is running Update after the call is completed, prior to the back of the yeild conditions are met, coroutines will always hang in there.

Different types corresponding to a yeild return different conditions:
(. 1) null, number, string: running will continue to run in the next frame after all function Update ()
(2) WaitForSeconds: after a specified delay time, all Update ( ) continues to run after run function
(3) WaitForFixedUpdate: continue to run after the function runs in all FixedUpdate (). (Note that this does not mean the next frame, because FixedUpdate () frame rate and Update () is not the same)
(3) WWWW: When the WWW continue to run resource loading. (Other asynchronous resource loading function is the same)

StartCoroutine: continue to run after run completion of nested coroutine.

If you will continue to achieve in-depth study coroutine, you can see the "CLR via C #" and "in-depth understanding of C #" two books in the iterator implementation principles underlying Unity is achieved through its coroutine.

Unity, the coroutine a typical follows:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        StartCoroutine(FuncA());
    }

    IEnumerator FuncA() {
        Debug.Log("Log1");
        yield return new WaitForSeconds(1.0f);
        Debug.Log("Log2");
        yield return StartCoroutine(FuncB());
        Debug.Log("Log3");
    }

    IEnumerator FuncB() {
        Debug.Log("Log4");
        yield return new WaitForSeconds(2.0f);
        Debug.Log("Log5");
    }
}

Output:

Log1
(等待1s)  
Log2  
Log4  
(等待2s)  
Log5  
Log3

Guess you like

Origin blog.csdn.net/shirln/article/details/95215720