Home > OS >  Optimizing Disabled Game Objects in Unity
Optimizing Disabled Game Objects in Unity

Time:01-30

I am creating a infinite terrain generation system in Unity that instantiates "chunks" of terrain around the player and deactivates them once they are no longer visisble. (See images bellow)

Chunks of terrain are loaded around the player

After the player wanders around for a bit many of these chunks will be instantated and later have thier GameObjects deactivated. My problem is that all of the deactivaed chunks sitting in the scene are taking up computational resources.

Now the obvious solution would be to destroy the chunks that are no longer visible so that unity no longer loads them, however the chunks need to exist in memory in order to be re-enabled when the player is close enough to enable them again. This would work, however each chunk that gets loaded has data associated with it that needs to stay persistant.

Is there any way to optimize disabled GameObjects or Destroy GameObjects and keep their data?

CodePudding user response:

I assume by disabled GameObjects you mean that they're disabled with GameObject.SetActive(false), which unfortunately is the most practical/optimal way of doing it while keeping it in RAM. What you'll have to do is serialize the chunk so that you can save it to the disk, so that you can load it later when the player gets close to it again.

There is no simple way of doing this, you'll just have to manually figure out exactly what you need to save, write a function that can save a chunk with this data, and a different function to generate a chunk from this data.

  • Related