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.
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.
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.
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.
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?