【问题标题】:Unity - How to run 2 different IEnumerator Methods at the same time?Unity - 如何同时运行 2 个不同的 IEnumerator 方法?
【发布时间】:2021-01-04 00:11:37
【问题描述】:

假设您有一个 IEnumerator 方法,它使用 time.deltatime 缓慢降低 hp 条,然后另一个 IEnumerator 方法降低包含当前 hp 数字的 hp 文本,这样您就有了一个很好的动画效果栏和健康号码。

但是当你调用它们时做这样的事情,它只会一个一个地运行它:

    //updating the hp stat and UI Text
    yield return UpdateHP();
    //Updating the HP Bar gameobject using transform.localScale.x
    yield return UpdateHPBar();

我不是 IEnumerator 方法/协程方面的专家,但有什么方法可以告诉它同时运行两个 IEnumerator 方法?感谢您的帮助。

还希望提供一些关于如何同时运行两个 IEnumerator 方法的示例。

【问题讨论】:

  • 您的示例与 Unity Coroutines 没有任何关系。 Unity Coroutines only yield return 协程继续之前的延迟,您的示例代码完全是另一回事
  • 我的问题是如何同时运行两个协程,没有代码,我只想知道如何完成这样的事情的概念。我的例子只是为了展示如果我调用这两个方法,订单将如何执行
  • 我稍微改了一下标题,希望能澄清一下,如果我没有正确描述它,请见谅
  • 如果你只返回一个元组呢? yield return (updateHP(), updateHPBar())
  • 协程本质上是(伪)并行运行的。您只需要同时启动它们。 void DoThings() { StartCoroutine( Coroutine1(data) ); StartCoroutine( Coroutine2(data) ); }

标签: c# unity3d


【解决方案1】:

这已通过 Immersive 在 cmets 中解决

协程本质上是(伪)并行运行的。你只需要同时启动它们。 void DoThings() { StartCoroutine( Coroutine1(data) ); StartCoroutine( Coroutine2(data) ); }

但是,如果有人想展示替代方案或更好的方法,请随时发布。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多