- Home /
How do I render infinite 2D terrain using the Unity's Tilemap class?
Hi, In my 2D project, I create an infinite terrain using some perlin functions (similar to how it's done in Minecraft or Factorio). Now I need to render this terrain. I plan to do this using the Unity's Tilemap class. Obviously I can not draw the entire world as it is infinite. Therefore I can only draw certain tiles of the world; the ones that the camera is currently looking at.
Here's what I'm concerned about: how can I do this without leaving any memory leaks behind? Since the camera is going to be moving around a lot - should I also clear the tiles that the camera is no longer looking at? (or does Unity already do this?) If I need to clear no-longer visible tiles, what's the correct way of doing it to make sure they get garbage-collected? Is it enough to just do:
tilemap.SetTile(new Vector3Int(x, y, z), null);
Is there something else I'm forgetting? Is Unity's Tilemap built to handle infinite tilemaps?