Unity coroutines (coroutines)

The general use of coroutines is to avoid new energy overhead and reduce the use of update. When there are many timers, there is no need to define many float variables, and one coroutine can solve it.

Original text: http://www.unClick to open the link ity.5helpyou.com/2658.html

2. Features of coroutines

1. The coroutine suspends execution when the interrupt instruction (YieldInstruction) is generated

2. As soon as the execution of the coroutine is suspended, it will return immediately//After interrupting the coroutine, return to the main function, and continue to execute the remaining functions of the coroutine after the suspension is over.

3. After the interrupt instruction is completed, continue execution from the next line of the interrupt instruction

4. At the same time, there can be multiple suspended coroutines in a script instance, but only one running coroutine

5. After the function body is all executed, the coroutine ends

6. The coroutine can well control the behavior executed after a certain number of frames

7. In terms of performance, coroutines have almost no more overhead than general functions


  Instruction Description Implementation

WaitForSeconds waits the specified number of seconds yield return new WaitForSeconds(2);

WaitForFixedUpdate waits for a fixed frame yield return new WaitForFixedUpdate();

WaitForEndOfFrame waits for the frame to end yield return new WaitForEndOfFrame();                         

StartCoroutine waits for a new coroutine to pause yield return StartCoroutine(other coroutine);

WWW waits for a load to complete yield return www;

Notice:

1. In a coroutine A, another coroutine B is started in the interrupt instruction, and the order of execution when yield returns StartCoroutine is:

①: Execute the new coroutine B first;

②: After the new coroutine B is suspended, it returns to the coroutine A, the coroutine A is suspended, and returns to the upper-level function of the coroutine A;

③: Because the sign that determines whether the coroutine A ends is whether the new coroutine B ends, so when the new coroutine B ends, return to the coroutine A to continue to execute the rest of the content;

④: The execution of coroutine A ends.

//execute every 3 seconds
    while(true){
        //to do something
        yield return new WaitForSeconds(3);
    }

8. Examples

lg1, give an example to illustrate the execution flow of the coroutine

 

using UnityEngine;
using System.Collections;

public class SimpleCoroutine : MonoBehaviour {
/// <summary>
/// Start, 协程的执行流程
/// Start函数运行,输出“1”,然后开始协程Do;
/// Do输出“2”,然后开始协程newDo;
/// newDo输出“3”,产生中断指令后暂停,立即返回Do;
/// Do产生中断指令后暂停,Do暂停并立即返回Start函数;
/// Start执行StartCoroutine的下一条语句:输出“4”;
/// 2秒后,newDo的中断指令完成并继续执行,输出“5”,协程newDo结束;
/// Do的中断指令因为协程newDo的结束而完成并继续执行,输出“6”,协程Do结束。
/// </summary>
void Start () {
Debug.Log(“1”);
StartCoroutine(Do());
Debug.Log(“4”);
}
IEnumerator Do() {
Debug.Log(“2”);
yield return StartCoroutine(newDo());//WaitForSeconds(5);
Debug.Log(“6”);
}
IEnumerator newDo() {
Debug.Log(“3”);
yield return new WaitForSeconds(2);
Debug.Log(“5”);
}
}
//输出结果顺序是,1,2,3,4,5,6

[/csharp]

lg2、加载指令(通过WWW加载本地文件)

?
1
 

private string path = “file://F:/Resource/Dragon.unity3d”;
void OnGUI(){
if(GUI.Button(new Rect(200,200,150,30),”点击进入协同程序”)){
Debug.Log(“1”);
StartCoroutine(loadLocalBundle(path));
Debug.Log(“3”);
}
}
private IEnumerator loadLocalBundle(string url){
Debug.Log(“2”);
using(WWW www = new WWW(url)){
yield return www;
Debug.Log(“4”);
if(www.error != null){
var bytes = www.bytes;
}
AssetBundle ab = www.assetBundle;
GameObject gameObject = ab.mainAsset as GameObject;
Instantiate(gameObject);
Debug.Log(“5”);
Debug.Log(“load local assetBundle finished…”+gameObject);
}
}
注意:
大概执行流程,点击按钮后开始执行协同程序,WWW按照提供的url进行加载,完毕后 yield return www;中断指令跳转到主线程。
主线程继续执行其他内容,www在加载完成后跳出中断继续执行余下内容。
加载完毕,实例化加载内容。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270113&siteId=291194637