The Problem:

I am making a turn based game and at the end of a players turn a complex script is run before it is the players turn again.

The problem is that when this complex script is run the game freezes for about 5 to 8 frames, this is only going to get worse as I add more complexity to the game.

As it is a turn based game it will not technically be a problem for the game to freeze for a few seconds between turns but it does look bad, especially since all the characters idle animations freeze and the game looks crashed.

The Question:

How can I get this complex script to run in the background “over a few frames” once a players turn is over, while still maintaining a frame rate that allows animations to play?

I will obviously not permit inputs during this period and nothing will change “except whats happening in the complex script”, other than the idle animations playing.

Bad Solutions:

I could display a “Loading” screen before running the script and then remove it when the script has completed but that will look just as bad and kill immersion.

You could make use of coroutines. You would have to modify your code to allow the game to “catch up & do other stuff” while your operation is processing.

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

You could make use of threads to run your code on a different core of your processor (or switch between tasks on single core architectures).

Take care if you do threads as you can’t access most Unity classes from a different thread than the main thread.

Also you could consider trying to optimize your code.