Running Costly Code over Time

Hello mateys,

In short, I have a relatively computationally complex set of code for generating terrain on the fly that currently costs too much to run as a single function call from the Update() when conditions are filled i.e.

void Update(){

    if(some_condition){

    Some_Void_Function();

    }

}

I have optimised my code to the best of my ability, but still find the game hangs briefly when calling this function once the condition is fulfilled.

I predict that with my limited skills and resources I cannot do it more cheaply than it stands at the moment. However, I am lucky in that I have plenty of time between function calls. As such, is there some way within C# of making the code run bit by bit over time rather than in one fell swoop to decrease the spike in processing when I call the function?

Many thanks,

Popup Pirate

Coroutines.