Unity WaitForFrames in Coroutine

Unity3D WaitForFrames in Coroutine

If you are using Coroutines in Unity3d, you probably know about WaitForSeconds, WaitForEndOfFrame and WaitForFixedUpdate classes. Here is an example:
public IEnumerator CoroutineAction()
{
    // do some actions here    
    yield return new WaitForSeconds(2); // wait for 2 seconds
    // do some actions after 2 seconds
}

But sometimes you need to wait for some amount of frames before executing some action. I have created simple class to help you with that:
public static class WaitFor
{
    public static IEnumerator Frames(int frameCount)
    {
        if (frameCount <= 0)
        {
            throw new ArgumentOutOfRangeException("frameCount", "Cannot wait for less that 1 frame");
        }
 
        while (frameCount > 0)
        {
            frameCount--;
            yield return null;
        }
    }
}

Now you can wait for frames:
public IEnumerator CoroutineAction()
{
    // do some actions here    
    yield return StartCoroutine(WaitFor.Frames(5)); // wait for 5 frames
    // do some actions after 5 frames
}

Unity3D WaitForFrames in Coroutine