ScreenCapture.CaptureScreenshotAsTexture() each 5 seconds

Hello, I’m using the ScreenCapture.CaptureScreenshotAsTexture() function to take screencapture. I’m using this coroutine (the one from the documentation)

    IEnumerator RecordFrame()
    {
        yield return new WaitForEndOfFrame();
        var texture = ScreenCapture.CaptureScreenshotAsTexture();
        // do something with texture

        // cleanup
        Object.Destroy(texture);
    }

    public void LateUpdate()
    {
        StartCoroutine(RecordFrame());
    }

The thing is that LateUpdate is too often for me, I need something “slower”. I don’t know if making a coroutine inside a coroutine is a good thing, but nonetheless I tried and it seems to fire more than once each 3 seconds anyway. Any idea how to get a screenshot each 3 seconds?
IMPORTANT! Using a second camera and rendering it to a texture IS NOT AN OPTION!

Found the answer to my own question. Make another function that calls the RecordFrame coroutine and call this function via InvokeRepeating.
Calling a coroutine that wait 3 seconds from LateUpdate doesn’t work because the coroutine is async. That means that regardless of the 3 seconds being finished, LateUpdate is still being called every frame, so it’s just calling a waitforseconds every frame regardless.

Alternatively you can just have the coroutine loop. You don’t need additional coroutines or the InvokeRepeating().

IEnumerator RecordFrames()
{
    while (true)
    {
        yield return new WaitForSeconds(5.0f);
        yield return new WaitForEndOfFrame();

        var texture = ScreenCapture.CaptureScreenshotAsTexture();

        // do stuff

        Destroy(texture);
    }
}
1 Like

Cool, thank you!