以下是两个功能的示例。一个使用Update
,另一个使用IEnumerator
。我知道每帧都会调用Update
函数。 我的问题: 每个帧都会调用IEnumerator
吗?
注意:我使用的是yield WaitForSeconds
,而不是yield WaitForEndOfFrame
或yield null
。
更新功能(示例1):
//_ratePerSecond is how often the functions must execute
void Update()
{
if ((CurrentPoints >= MinPoints) && Time.time >= _timeToChangeValue)
{
_timeToChangeValue = Time.time + _ratePerSecond;
CurrentPoints += 1
}
}
IEnumerator函数(示例2):
//this is called with StartCoroutine(ChangeValue());
IEnumerator ChangeValue()
{
while (true)
{
yield return new WaitForSeconds(_ratePerSecond);
if (CurrentPoints >= MinPoints)
{
CurrentPoints += 1;
}
}
}
更新
以下是在示例1和1中运行功能的10000个对象完成的性能测试。 2.以红色突出显示的是Update
功能(示例1),以绿色突出显示为IEnumerator
功能(示例2)。
性能测试的结论:Update
函数的效率较低。当延迟更大时,IEnumerator
表现得更好。
答案 0 :(得分:1)
IEnumerator还调用了每一帧吗?
在您的问题中使用协同程序功能,答案是否。
WaitForSeconds
函数将暂停 ChangeValue()
函数,直到等待指定的(_ratePerSecond
)秒为止,然后它将执行代码while
循环,再次跳回while
循环的开头并再次暂停(_ratePerSecond
)秒。在StopCoroutine
循环中调用StopAllCoroutines
/ yield break
或while
后,此操作将重复停止。
以下是Update
函数中代码的协程版本。
IEnumerator ChangeValue()
{
while (true)
{
if ((CurrentPoints >= MinPoints) && Time.time >= _timeToChangeValue)
{
_timeToChangeValue = Time.time + _ratePerSecond;
CurrentPoints += 1;
}
//Wait for a frame
yield return null;
}
}
请注意,您必须使用StartCoroutine(ChangeValue());
而不是ChangeValue();