- Home /
2D Tilemap which way to go - memory-wise
I'm working on a 2d platformer and using a 2d tilemap structure.
What I'm currently doing is instantiating tile gameObjects only inside visible camera area, and destroying tiles that are not being seeing.
I've read somewhere though that when dealing with texture objects, automatic garbage collection is not enough.
I don't remember what the catch was, though.
Can someone enlighten me about this?
I guess it'd be important to know this regarding every other gameObject, actually (enemies DO have sprites etc.)
Using seperate GameObjects for tiles is generally not effective. Use a mesh ins$$anonymous$$d.
Destroying and Instantiating GOs all the same is also not good, it leads to memory build-ups. If you have to do it, use some kind of objects pooling where you can reuse objects.
Thanks for the answer!
Regarding 1: So to make sure I did understand this, I would use one big mesh as the whole scene, right?
Regarding 2: Instantiating and destroying, let's say, shots would be a huge no-no then? I should always be reusing these objects?
$$anonymous$$aybe not one mesh for the whole scene, but you can divide it into sections to be meshified.
With any reusable or frequently spawned object, it's always a good idea to pool and reuse them.
Answer by tuncOfGrayLake · May 06, 2016 at 06:13 PM
I highly recommend you consider one of the existing tile system packages on the Asset Store. It will save you a lot of headache just for the price of a McDonald's takeout.