从一个协程内部到另一个协程的调用是否需要 StartCoroutine?
Is a StartCoroutine needed for a call from inside one co-routine to another co-routine?
当你有像
这样的嵌套协程时
void Update()
{
if(someTest)
{
StartCoroutine(Foo());
}
}
IEnumerator Foo()
{
doStuff = true;
yield return StartCoroutine(Bar());
doStuff = false;
}
IEnumerator Bar()
{
//Very important things!
}
yield return StartCoroutine(Bar());
中的StartCoroutine
有必要吗?
我们可以做
void Update()
{
if(someTest)
{
StartCoroutine(Foo());
}
}
IEnumerator Foo()
{
doStuff = true;
yield return Bar();
doStuff = false;
}
IEnumerator Bar()
{
//Very important things!
}
如果允许的话,这对程序有影响吗behavior/performance?
Is the StartCoroutine in yield return StartCoroutine(Bar());
necessary?
否,允许使用yield return Bar();
.
If we are allowed, does this have any impact on the program
behavior/performance?
对行为和性能问题是。
差异:
yield return StartCoroutine(coroutineFunction())
:
- 内部协程(
Bar
) 将在 产生 之前启动
- 内存分配:56字节
- 通话次数:2
- 当父协程被杀死时,子协程被杀死
从
StartCoroutine
开始继续到 运行。
yield return coroutineFunction()
:
- 内部协程(
Bar
) 将在 产生 之后启动
- 内存分配:32字节
- 通话次数:3
- 当父协程被杀死时,子协程被杀死
以
yield return coroutineFunction()
开头的也被杀死了。
了解这一点非常重要,尤其是当您需要停止
父协程及其子协程。
更快:
可能是因为它分配的内存较少。在 for
循环中使用时
它比 yield return StartCoroutine(coroutineFunction())
快。
即使它有更多的调用也是如此。此外,Time 和
Profiler 中的 Self ms 显示其值小于 yield return StartCoroutine(coroutineFunction())
.
中的值
总结:
yielding
的区别几乎类似于 i++
与 ++i
(post 和预增量)。如果您关心内存管理,请使用第二种方法 yield return coroutineFunction()
,因为它分配的内存较少。此外,如果您希望能够在父协程停止时停止所有内部或子协程,那么也可以使用 yield return coroutineFunction()
.
当你有像
这样的嵌套协程时void Update()
{
if(someTest)
{
StartCoroutine(Foo());
}
}
IEnumerator Foo()
{
doStuff = true;
yield return StartCoroutine(Bar());
doStuff = false;
}
IEnumerator Bar()
{
//Very important things!
}
yield return StartCoroutine(Bar());
中的StartCoroutine
有必要吗?
我们可以做
void Update()
{
if(someTest)
{
StartCoroutine(Foo());
}
}
IEnumerator Foo()
{
doStuff = true;
yield return Bar();
doStuff = false;
}
IEnumerator Bar()
{
//Very important things!
}
如果允许的话,这对程序有影响吗behavior/performance?
Is the StartCoroutine in yield return StartCoroutine(Bar()); necessary?
否,允许使用yield return Bar();
.
If we are allowed, does this have any impact on the program behavior/performance?
对行为和性能问题是。
差异:
yield return StartCoroutine(coroutineFunction())
:
- 内部协程(
Bar
) 将在 产生 之前启动
- 内存分配:56字节
- 通话次数:2
- 当父协程被杀死时,子协程被杀死
从
StartCoroutine
开始继续到 运行。
yield return coroutineFunction()
:
- 内部协程(
Bar
) 将在 产生 之后启动
- 内存分配:32字节
- 通话次数:3
- 当父协程被杀死时,子协程被杀死
以
yield return coroutineFunction()
开头的也被杀死了。 了解这一点非常重要,尤其是当您需要停止 父协程及其子协程。 更快:
可能是因为它分配的内存较少。在
for
循环中使用时 它比yield return StartCoroutine(coroutineFunction())
快。 即使它有更多的调用也是如此。此外,Time 和 Profiler 中的 Self ms 显示其值小于yield return StartCoroutine(coroutineFunction())
. 中的值
总结:
yielding
的区别几乎类似于 i++
与 ++i
(post 和预增量)。如果您关心内存管理,请使用第二种方法 yield return coroutineFunction()
,因为它分配的内存较少。此外,如果您希望能够在父协程停止时停止所有内部或子协程,那么也可以使用 yield return coroutineFunction()
.