I am in the process of trying to create a game similar to Terraria or Starbound. I have been trying out different methods of optimizing map creation over the last week. Somebody in the forums stated to me they could create a 4096 x 4096 map that only uses 128 MB memory total (according to Unity profiler) by using pooling.
Pooling is a new concept to me, and I have done some reading and checking out some tutorials on it. I think…I understand how it could be used, but the second I tried to use it I wound up with a 384 x 384 map that uses 750 MB memory and takes 30 seconds to load. So I obviously must be doing something wrong, because so far pooling appears to be crap.
Here is what I have done. I have 3 prefabs, Map, Chunk, and Tile. The tile prefab is a GameObject with a SpriteRenderer and Box Collider 2D. The chunk is an empty game object meant to serve as a parent to the tiles it contains (for organizational purposes). It contains a script that includes tile width, tile height, and a 2D tile array. On load, it initializes the tile array and instantiates all tiles it contains. Finally, the Map is an empty gameobject that holds chunks. It has a script attached that includes max chunks to create. On load it instantiates chunks up to its max (which in turn causes the chunks to instantiate their tiles).
The goal is that there will be a chunk above, below, left, right, upper left, upper right, lower left, lower right, of the player, including the chunk the player is in. As the player heads in a direction, the chunk gets moved to be ahead of the player. So I will never need more than 9 loaded at a time (unless I go multiplayer), and the player should in theory, be able to move in any direction endlessly.
What values the tiles on the map have (i.e. air, dirt, etc. and other info) is dictated by a MapData script. When the chunks move, the tiles within the moved chunk set themselves based on the MapData of the tile located at its position.
So that’s my theory. Can anybody tell me where I might be misunderstanding the concept, or misusing it in any way? When the game releases, I want a load time of 1 minute or less for a computer that meets the minimum requirements. So having a map that takes 30 seconds to generate (without grass, trees, physics, enemies, npcs, etc.) on a beast computer is unacceptable.