IEnumerator UpdateChunks() { for (;;) { //Wait for the initial chunks to get loaded and load them all at once. if (chunksToUpdate.Count > 0 && firstRun) { Debug.Log("only once"); while (chunksToUpdate.Count > 0) { firstRun = false; popAndGenerate(); } } if (chunksToUpdate.Count > 0) { popAndGenerate(); } if (chunksToRefresh.Count > 0) { Chunk chunk = chunksToRefresh.First.Value; chunksToRefresh.RemoveFirst(); if (chunk != null) { chunk.updateChunk(); } } //Might need to unload chunks that have been around for awhile ChunkWatcher chunkWatcher = unloaderQueue.Peek(); if (chunkWatcher != null) { if (Time.realtimeSinceStartup - chunkWatcher.timeSinceActive > 30) { unloaderQueue.Remove(); float distance = Vector3.Distance(player.transform.position, chunkWatcher.chunk.gameObject.transform.position); if (Math.Abs(distance) > unloadBlockDistance) { chunkWatcher.chunk.gameObject.SetActive(false); chunkWatcher.region.unloadChunk(chunkWatcher.x, chunkWatcher.y, chunkWatcher.z); inactiveChunkPool.AddLast(chunkWatcher.chunk.gameObject); } else { //update time stamp and put back in queue. chunkWatcher.timeSinceActive += 30; unloaderQueue.Add(chunkWatcher); } } } yield return(new WaitForSeconds(renderSpeed)); } }
public void addToUnloadQueue(Region region, Chunk chunk, int x, int y, int z) { ChunkWatcher watcher = new ChunkWatcher(region, chunk, x, y, z); unloaderQueue.Add(watcher); }