GameObject object-pooling in tilemaps

I guess I have 2 questions:

  1. Does tilemap do any object-pooling for GameObjects instantiated by tiles? My guess is no since I’m seeing a lot of Instantiate calls in the profiler.
  2. If the answer to #1 is no, then is there a feasible way to use object-pooling with tilemaps? Seems like instantiation is hidden from the API and the first access to the instantiated GameObject is in the Tile.StartUp.

Any help or idea is much appreciated.

Thank you.

Idea: You could position GameObjects on the Grid manually.

var worldPos = grid.CellToWorld(cell)
var cellPos = grid.WorldToCell(pos)

Adjust to center as appropriate. Register them to a parallel data structure for lookups. Dictionary<Vector3Int, GameObject> if a dynamically-sized world, or an array if a static world. Then use use your own pooling solution. How you manage registration/de-registration with pooling may require some thought.

Thanks for the suggestion!
Though, I’m not sure if I understand it properly. It seems you are suggesting that I should just roll out my own tilemap solution? If possible I’d like to stick with Untiy 2D tilemaps.

Yes, I’m suggesting not using the tilemap: create your own collection parallel to it. Perhaps you could try to integrate it, but I can’t guarantee the success of this because as I recall the hooks (like StartUp) you have for changing the G.O.s on the tilemap are limited. I went with this solution after I found working with those hooks frustrating.

I guess not using Unity’s 2D Tilemap is an option. But I considered it a last resort since it’s quite some work to replace it with a new solution.

I was hoping there’s some way to tell Unity 2D Tilemap on how to instantiate new GameObjects.

Trying to fit a square peg in a round hole is going to be much more painful in the long run than going custom.

are you literally trying to have 1 game object for every tile?

Because the point of the tilemaps is to avoid that, its normal that the tilemap feature does not have a solution for that since it was created specifically as an alternative to it

I have one tilemap that has terrain tiles (sprite only) for every tile.
I have another tilemap for GameObjects that has more involved behaviours (imagine a top down game with interactable objects such as trees, rocks, firepits, bed, etc). So the tilemap could have 1 game object per tile, but in practice, it won’t.

I think I’ll consider Lo-renzo’s suggestion of just pulling the GameObjects out of the tilemap and manage it manually under a Grid.

Thanks all for the input :slight_smile:

  1. Unfortunately, it does not using object pooling.
  2. The alternative would be not to set a GameObject to instantiate in the Tile itself, but to script the creation of the GameObject/s using object-pooling with a scripted TileBase.StartUp call. Doing so would give you the Tilemap and the position as parameters with a null instantiated GameObject.

A followup question would be: what would your expectations be of an object-pooling system with the Tilemap?

Would it be possible to share the hooks you would like to see or any improvements to the existing ones?

Thanks!