- Home /
What to do with "unloaded" gameObjects?
I'm working on a small 2D project that has a character moving around in a grid of tiles in a top-down fashion. I'm using chunks to reduce the processing load because it freezes a lot when generating large worlds and it worked; I have set up those chunks (GameObjects with tiles as children) with their data about held tiles (GameObjects with a Sprite Renderer and attached script) and they are managed in Lists by a world manager. I understand how to load the chunks near the player and all, but my question is:
When my character is somewhere in the game, and he has chunks loaded, what do I programmatically do with chunks that shouldn't be loaded anymore, for example the player moves away? They get removed my List of active chunks but where should I let them "go" to "unload" them.
I was thinking of disabling the GameObject of that chunk, and re-enabling it when the player is near but other than that I have no idea. My goal here is to optimize performance and unload unused chunks with their data for my to be large game world.
Maybe I'm missing something very important here but I just can't seem to wrap my head around a way of doing this.
Answer by TimHeijden · Aug 04, 2015 at 01:19 AM
It sounds like you're on the right track. What you are describing is almost exactly the same as pooling objects. There are a lot of posts, blogs, videos about it all over the interwebs :)
With object pooling, you create an initial amount of objects and use those throughout your game without having to create/destroy (load/unload) them all the time. Instead, you set the values of the objects whenever they are enabled or disabled.
Here is a non-descriptive but visual example of some dude who did something like this: http://jessefreeman.com/dev-diary/tile-map-editor-object-pooling/ Note the bottom GIF, where you see the tiles on the left get "disabled" and "enabled" on the right.
That sounds like a good way, but it wouldn't be feasible with a generated terrain, there aren't a lot of "same" gameobjects in that aspect.
I searched a little more and found that I can serialize the tiles held by the chunk, and load them when they need to, but I'm worried about making loads of I/O operations on the disk..
You'll have to rework what you have to work as if they are the "same" gameobjects. I don't have details on your current implementations & what the tiles should be doing, but it's pretty much always the way to go.
Like you said, the alternative would be doing lots of loading / unloading of gameObjects but that kills your performance...