C# Custom (slower) Update function?

I have a top down 2-d game, that generates new chunks as needed, depending on how close the player is to the current edge of the map. (Pseudo code below)

//Begin Pseudo code


int ChunkSize;

//How far should I look to generate new chunks

int ChunkRadius

//The position of my current chunk

Vector3 CurrentChunk;

//A list of all my chunks

List  ChunkPositions = new List();

void PlayerToChunkCoords(Vector3 playerposition)
{
    //Convert the players position to a set of chunk coordinates 

}
void CheckAndGenerateChunks()
{
//Get the chunk coordinates

//use for loops and the radius to check if all of the chunks in range have been generated

//if our list doesnt have a chunk within the radius, generate it and add its coordinates to the list
}

///End pseudo code

This code doesn’t need to run every frame, and putting it in fixedupdate (and slowing down fixedupate) gives me a huge gain in fps. But I really want to be able to take it out of fixedupdate, and put it on its own cycle.

If you need it to be consistently run at some frequency (and I mean consistent like it’s always exactly every 1 second), then put it in FixedUpdate(), but wrap it in a “time to go” test like this:

float timeToGo;

void Start() {
    timeToGo = Time.fixedTime + 60.0f;
}

void FixedUpdate() {
    if (Time.fixedTime >= timeToGo) {
        // Do your thang
        timeToGo = Time.fixedTime + 60.0f;
    }
}

In this example, it would run your code every 60 seconds.

You can alternatively, though less efficiently, use a coroutine like so:

void Start() {
    StartCoroutine(work());
}

IEnumerator work() {
    while (true) {
        yield return new WaitForSeconds(60.0f);
        // Do some work
    }
}

This will be a bit less efficient, because you have the overhead of the coroutine mixed in with a few extra cycles to yield for waitforseconds every frame.

You could also call InvokeRepeating(“CheckAndGenerateChunks”, 0, x) in your Start() function to call your code every x seconds.

Here is a simpler approach, using only one variable (the code inside Update will run every 3 frames in this example.):

private int updateInterval = 3; // in frames

void Update(){
	if (Time.frameCount % this.updateInterval != 0) return;
	
	// your code here...
}

With this, you use only you extra variable on your code, and put just one extra line on your Update() loop.

The other reason that this is better than initializing Coroutines on Start() is because when you do this and your game object gets disabled and enabled again, you Coroutine will not run. Of course, you could initialize it OnEnable(), but it’s better to just use 2 extra lines with only one new variable than allocate memory and processing time with coroutines.

See this method of achieving a companion Update that you can call as often as you want.

While you may not be interested in being able to perform functions step-by-step you could modify CoStart to call your custom Update function every x seconds or every x events.

http://wiki.unity3d.com/index.php?title=CoUpdate

You can have a constant update and chunk generation entirely using coroutines:

void Awake()
{
    StartCoroutine(ConstantUpdate);
}

IEnumerator ConstantUpdate()
{
   while(true)
   {
        // come code
       if(m_IsGameRunning)
       {
           // wait for chunks to update
           yield return  UpdateChunks();
       }
       yield return null;
   }
}

IEnumerator UpdateChunks()
{
     for(int i = 0; i < chunks.Count; i++)
    {
        // stuff
       yield return null;
    }
}