Joh*_*ada 9 c# unity-game-engine
我知道Unity3D StartCoroutine调用一个与StartCoroutine在同一个线程上运行的函数,但被调用的函数何时返回原始调用者?
Joh*_*ada 23
我环顾互联网寻找一个很好的Unity3D Coroutine示例,却找不到完整的示例.UnityGems 有一个很好的解释,但即使他们的例子也不完整.所以我写了自己的例子.
这个:
using UnityEngine;
using System.Collections;
public class MainCamera: MonoBehaviour {
void Start () {
Debug.Log ("About to StartCoroutine");
StartCoroutine(TestCoroutine());
Debug.Log ("Back from StartCoroutine");
}
IEnumerator TestCoroutine(){
Debug.Log ("about to yield return WaitForSeconds(1)");
yield return new WaitForSeconds(1);
Debug.Log ("Just waited 1 second");
yield return new WaitForSeconds(1);
Debug.Log ("Just waited another second");
yield break;
Debug.Log ("You'll never see this"); // produces a dead code warning
}
}
Run Code Online (Sandbox Code Playgroud)
生成此输出:
About to StartCoroutine
about to yield return WaitForSeconds(1)
Back from StartCoroutine
Just waited 1 second
Just waited another second
Run Code Online (Sandbox Code Playgroud)