C# 是';IEnumerator';在每个帧上使用';产生等待秒数';

C# 是';IEnumerator';在每个帧上使用';产生等待秒数';,c#,unity3d,C#,Unity3d,下面是两个函数的示例。一个是使用Update,另一个是使用IEnumerator。我知道,Update函数在每一帧都被调用我的问题:IEnumerator也被称为每帧吗 注意:我使用的是yield WaitForSeconds而不是yield WaitForEndOfFrame或yield null 更新功能(示例1): //_ratePerSecond is how often the functions must execute void Update() { if ((Curren

下面是两个函数的示例。一个是使用
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
    }
}
//this is called with StartCoroutine(ChangeValue());
IEnumerator ChangeValue()
{
    while (true)
    {
        yield return new WaitForSeconds(_ratePerSecond);

        if (CurrentPoints >= MinPoints)
        {
            CurrentPoints += 1;
        }
    }
}
IEnumerator函数(示例2):

//_ratePerSecond is how often the functions must execute
void Update()
{
    if ((CurrentPoints >= MinPoints) && Time.time >= _timeToChangeValue)
    {
        _timeToChangeValue = Time.time + _ratePerSecond;
        CurrentPoints += 1
    }
}
//this is called with StartCoroutine(ChangeValue());
IEnumerator ChangeValue()
{
    while (true)
    {
        yield return new WaitForSeconds(_ratePerSecond);

        if (CurrentPoints >= MinPoints)
        {
            CurrentPoints += 1;
        }
    }
}
更新:

//_ratePerSecond is how often the functions must execute
void Update()
{
    if ((CurrentPoints >= MinPoints) && Time.time >= _timeToChangeValue)
    {
        _timeToChangeValue = Time.time + _ratePerSecond;
        CurrentPoints += 1
    }
}
//this is called with StartCoroutine(ChangeValue());
IEnumerator ChangeValue()
{
    while (true)
    {
        yield return new WaitForSeconds(_ratePerSecond);

        if (CurrentPoints >= MinPoints)
        {
            CurrentPoints += 1;
        }
    }
}
下面是对运行示例1和2中函数的10000个对象进行的性能测试。红色突出显示的是
Update
功能(示例1),绿色突出显示的是
IEnumerator
功能(示例2)

使用0-1秒的随机延迟:

使用1-2秒的随机延迟:

性能测试得出结论:
更新
功能效率较低<当延迟较大时,code>IEnumerator的性能甚至更好

IEnumerator也叫每帧

使用问题中的协程函数,答案是否定的

函数将挂起
ChangeValue()
函数,直到它完成了对指定(
\u ratePerSecond
)秒的等待,然后它将在
while
循环中执行代码,再次跳回
while
循环的开头并将自身挂起一段时间(
\u rate persecond
)秒。这将重复执行,直到停止
停止协同程序
/
停止所有协同程序
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;
    }
}

请注意,您必须使用
start例程(ChangeValue());
而不是
ChangeValue()调用此函数使用
StartCourtine
运行
ChangeValue
,然后函数会等待几秒钟,那么它会在每一帧的哪里调用它?我不确定引擎是否会在每一帧调用枚举器。所以我要问的是:unity引擎是否会暂停IEnumerator的执行,并说“我已经完成了这个fra?”“me”,然后在下一帧中选择它停止的位置(如果有上一帧中的所有状态信息)需要注意的是,运行这样一个独立的测试并不能为您提供适用于真实场景的准确基准。
IEnumerator
的使用效率是否更高?考虑到上下文切换(“线程”的停止/启动)与
Update
功能相比,如果您需要按顺序执行某些操作,则在使用
IEnumerator
的每个帧上执行该功能。例如,从A移动到B。等待3秒钟。从B移动到A。是的,它绝对应该用于停止/启动任何操作,因为您不必在
更新中执行该操作将为每一帧运行的函数。