c# - 在返回 void 的函数中使用 yield WaitForSeconds()

标签 c# unity3d wait void yield-return

我的游戏中有跳跃恐吓。我正在使用 Unity 3D。 我的第一个功能是

public void ScareMe(Vector3 pos) {
    //it does some necessary irrelevant 
    //stuff and then it invokes another function
    Invoke ("Smile",.2f);
}

在我的另一个函数中,我想让一个对象在 0.2 毫秒内出现然后消失。 我用

 IEnumerator Smile() {
     Object.SetActive(true);
     yield return new WaitForSeconds(0.2f);
     Object.SetActive(false);
 }

但出于某种原因,只要它返回除 void 之外的其他内容,我的函数 Smile 就永远不会被调用。

有什么方法可以使用像 yield 这样的东西,但绕过它,这样我就不必返回任何东西了吗? 我在想像协程一样的 while 循环?但我不确定该怎么做。

最佳答案

如果我没记错的话,Unity 的 Invoke(func) 只愿意在 Javascript 中启动协程,其中 void 和协程之间的区别不太严格。

我会做的是使用 StartCoroutine(Smile()); 并用另一个 yield return new WaitForSeconds(0.2f); 启动 Smile,或者更好的是有 Smile采用 float smileDelay 参数并将其用于您的 WaitForSeconds。

关于c# - 在返回 void 的函数中使用 yield WaitForSeconds(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924204/

相关文章:

c# - 在 C# 中将具有变量名称/值对的 JSON 反序列化为对象

c# - 通过 ASP.NET 访问 Active Directory 记录

c# - Unity-如何检查主相机是否看到对象

c# - 为什么我在 Unity3D 的场景中从一个空的 GameObject 获得了 30 个绘制调用?

JavaScript/JQuery - 如何在使用后暂时禁用功能?

C# 提高 SQLite SELECT 性能

c# - 每个 C# 脚本都在控制台上抛出错误

timeout - 我怎样才能等到发送命令在expect脚本中完成执行

javascript - 通过超时在 Javascript 中休眠?

c# - 分页错误 :The method 'Skip' is only supported for sorted input in LINQ to Entities. 方法 'OrderBy' 必须在方法 'Skip' 之前调用