从一个协同例程内部调用另一个协同例程是否需要StartCoroutine? [英] Is a StartCoroutine needed for a call from inside one co-routine to another co-routine?

查看:88
本文介绍了从一个协同例程内部调用另一个协同例程是否需要StartCoroutine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您嵌套了

void Update()
{
    if(someTest)
    {
        StartCoroutine(Foo());
    }
}

IEnumerator Foo()
{
    doStuff = true;
    yield return StartCoroutine(Bar());
    doStuff = false;
}

IEnumerator Bar()
{
    //Very important things!
}

StartCoroutine yield中返回StartCoroutine(Bar()); 是否必要?

我们允许这样做吗?

void Update()
{
    if(someTest)
    {
        StartCoroutine(Foo());
    }
}

IEnumerator Foo()
{
    doStuff = true;
    yield return Bar();
    doStuff = false;
}

IEnumerator Bar()
{
    //Very important things!
}

如果允许的话,这是否会对程序的行为/性能产生影响?

If we are allowed, does this have any impact on the program behavior/performance?

推荐答案


StartCoroutine是否在收益回报率StartCoroutine(Bar());中?
是必需的吗?

Is the StartCoroutine in yield return StartCoroutine(Bar()); necessary?

,您可以使用 yield return Bar();


如果允许的话,这对程序$ b有什么影响? $ b行为/性能?

If we are allowed, does this have any impact on the program behavior/performance?

是行为和性能问题。

差异

yield return StartCoroutine(coroutineFunction())

yield return StartCoroutine(coroutineFunction()):


  • 内部协程( Bar )将在产生之前启动

  • 内存分配:56个字节

  • 调用:2

  • 当父协程被杀死时,子协程
    StartCoroutine 继续运行。

  • Inner coroutine(Bar) will be started before yielding
  • Memory Allocation: 56 bytes
  • Calls: 2
  • When a parent coroutine is killed, the child coroutine that is started with StartCoroutine continues to run.

yield return coroutineFunction()

yield return coroutineFunction():


  • 内部协程( B ar )将在 产生后开始

  • 内存分配:32字节

  • 呼叫:3

  • 当父协程被杀死时,子协程
    开始yield return coroutineFunction()也被杀死。
    这一点非常重要,尤其是当您需要与孩子停止使用
    父协程时。

  • 更快

  • Inner coroutine(Bar) will be started after yielding
  • Memory Allocation: 32 bytes
  • Calls: 3
  • When a parent coroutine is killed, the child coroutine that is started with yield return coroutineFunction() is killed as well. This is every important to know especially when you need to stop a parent coroutine with its children.
  • Faster:

可能是因为它分配的内存较少。在 for 循环
中使用时,它比 yield return StartCoroutine(coroutineFunction())更快。
即使调用次数更多,这甚至是正确的。此外,探查器中的 Time
Self ms 显示其值小于 yield return StartCoroutine(coroutineFunction() )

Probably because it allocates less memory. When used in a for loop it is faster than yield return StartCoroutine(coroutineFunction()). This is even true even though it has more calls. Also, Time and Self ms from the Profiler shows that its values are less than the ones from yield return StartCoroutine(coroutineFunction()).

总结

收益的区别几乎就像 i ++ vs ++ i (前后递增)。如果您关心内存管理,则使用第二种方法 yield return coroutineFunction(),因为它分配的内存更少。同样,如果您希望在父进程停止时停止所有内部或子程序,则也可以使用 yield return coroutineFunction()

The yielding difference is almost like the i++ vs ++i (post and pre increment). If you care about memory management then go with the second method yield return coroutineFunction() as it allocates less memory. Also if you want to be able to stop all the inner or child coroutines when the parent one is stopped then also use yield return coroutineFunction().

这篇关于从一个协同例程内部调用另一个协同例程是否需要StartCoroutine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆