本文介绍了在Unity3D StartCoroutine调用一个函数,是否函数返回时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Unity3D StartCoroutine调用运行于同一线程StartCoroutine一个功能,但是当它被调用函数返回到原始调用者?

I know that Unity3D StartCoroutine calls a function which runs on the same thread as StartCoroutine, but when does the called function return back to the original caller?

推荐答案

我看了看周围的互联网有一个良好的Unity3D协程的例子,找不到一个完整的。有一个的很好的解释,但即使是他们的榜样是不完整的。所以我写了我自己的例子

I looked around the internet for a good Unity3D Coroutine example and couldn't find a complete one. There is a great explanation by UnityGems, but even their example is incomplete. So I wrote my own example.

这:

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
  }
}



产生以下的输出:

Produces this output:

About to StartCoroutine
about to yield return WaitForSeconds(1)
Back from StartCoroutine
Just waited 1 second
Just waited another second

这篇关于在Unity3D StartCoroutine调用一个函数,是否函数返回时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:48