In Unity, how do I use the extra time in every frame to do some extra processing?

by Man   Last Updated January 17, 2018 17:13 PM

Let's say, for example, that my target framerate is 60 frames per second.

Now, I know that I have a maximum of 16.6 milliseconds to "do everything" every frame in order to maintain that framerate.

Let's say that I also have a background task that I wish to run. It will return some results. I don't need them urgently; I just wish to run it and have its results when it's done.

Here's my point. I know when I'm done rendering everything; WaitForEndOfFrame() in a coroutine tells me so. Let's say my last frame took only 13 milliseconds to render instead of 16.6, so I know I have 3.6 milliseconds to spare during which I would like to run my task for exactly those 3.6 miliseconds or less, never going over my 16.6 milliseconds maximum total, and then continue with the next frame, on which I will do the same if I get some spare time.

Could I achieve this using Unity's coroutines (or some other way)? If so, how?



Related Questions


Multi-threading for rhythm game?

Updated February 22, 2016 01:05 AM

Game Loop delay the right way and game speed

Updated February 20, 2017 20:13 PM


Return value from coroutine to non monobehaviour

Updated March 12, 2018 10:13 AM

How to await async operations / coroutines?

Updated July 26, 2016 08:05 AM