c# - Unity3D StartCoroutine调用一个函数,那个函数什么时候返回?

标签 c# unity3d

我知道 Unity3D StartCoroutine 调用一个与 StartCoroutine 在同一线程上运行的函数,但是被调用的函数什么时候返回到原始调用者?

最佳答案

我在互联网上四处寻找一个很好的 Unity3D 协程示例,但找不到完整的示例。有一个 great explanation由 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
  }
}

产生这个输出:

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

关于c# - Unity3D StartCoroutine调用一个函数,那个函数什么时候返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855983/

相关文章:

c# - 使用自定义 UserControl 作为 DataGridView 中的列

c# - C#.NET 中的 DDD "Module"

unity3d - 如何使游戏对象统一透明

algorithm - 离屏敌人指示器算法

c# - UI 管理框架 - 设计改进

c# - 仅由一个用户使用的虚拟目录上的 ServerTooBusyException

c# - 与访客的抽象树

C# 动态和扩展方法解决方案?

c# - 如何使用Unity Toggle Check Function(选中和未选中)

c# - 用于移动的 Unity C# 脚本不起作用