as coroutines are very important in optimization processes, making them compatible with a game paused state could be vital.
So here we go :
In your class containing those coroutines (or in any abstract/singleton you want), put these functions :
public Coroutine _sync(){
return StartCoroutine(PauseRoutine());
}
public IEnumerator PauseRoutine(){
while (_myPauseState) {
yield return new WaitForFixedUpdate();
}
yield return new WaitForEndOfFrame();
}
with _myPauseState being your global pause state.
then in any Coroutines, put this at the end (or at any place you want to check for a pause) :
yield return _sync();
I personally prefer to put the first code block into a generic baseClass, derived from MonoBehaviour , and then derive all my other âpause-ableâ classes from it. So I donât have to rewrite the block in all the new pause-able classes.
Iâm not sure if theyâre going to respond 4.5 years laterâŚ
Personally I donât use unity Coroutines, and instead I have my own âRadicalCoroutineâ that wraps around the unity Coroutine.
One feature of it is controlling how the coroutine pauses when the MonoBehaviour/GameObject it was called with gets enabled/disabled or activated/deactivated.
Here is the manager for said radicalcoroutines pausing:
What I usually do is I have a game state machine that will disable any critical gameobjects that get paused during âpauseâ mode.
Now, with all that⌠you may notice in the Start/Resume methods, it checks the most recent âIRadicalYieldInstructionâ and pauses/unpauses it.
Basically what I did was create a âWaitForDurationâ yield instruction:
This mimics the behaviour of âWaitForSecondsâ, BUT is pausible (and also scalable, using ITimeSupplier, which is something else I have in my framework). If you yield a WaitForDuration, or yield a WaitForSeconds in a coroutine that is pausible which then gets converted to a WaitForDuration, and the coroutine gets paused. This WaitForDuration doesnât count time for the duration of that pause.