- Home /
What is the correct way to create individual floor tiles in a 3d game?
I am creating a game that allows players to draw individual rooms on a 100x100 grid. The grid is visible when they draw their rooms as well as during the game play. The rooms can be irregular shapes but each 1x1 grid tile can not be divided. 1 tile, 1 space and any number of tiles to make up the room.
I am running a function afterwards to automatically place walls between different rooms. I am currently running through the following command to draw each cube. The result is 10,000 cubes though. If I hide the entire set of cubes my fps is over 70. If I make them visible it's more like 40 odd.
for(int i = 0; i < floorTiles.Count; i++) {
GameObject newTile = Instantiate(blankTile,new Vector3(floorTiles[i].x,0,floorTiles[i].y),transform.rotation,floor.transform);
}
The Instantiate part isn't running slow at all - it's quite quick. But the visual rendering of so many cubes seems to weigh Unity down too much. I am sure that I am just missing the concept somewhere of how to make this work and my best efforts of trying to find the answer online lead me more towards 2d projects.
So my question is what is the correct way to create individual floor tiles in a 3d game.
Have changed the cubes to quads with no texture just to test and it still drops right down in fps. It looks like (from someone on discord) that there should be no problem with this. Other complex games are running fine.
Answer by Captain_Pineapple · May 16, 2018 at 08:31 AM
Well first up you could use the builtin Unity Profiler to determin what actually takes up all the processing time. Sometimes the results can be surprising.
Secondly to solve your problem you might want to think of something like the following:
Change your 10000 floor Objects into one large Object with one Collider and a texture that simulates a grid. When you do a raycastings (which i assume you do) to determine the targeted tile you can simply calculate the rounded position of the hit-point of your raycast. There you can either:
Use a Projector to project a given tile graphic/texture on the current position.
Or Instantiate a quad like mentioned by @seandolan with the tile-texture applied.
I hope this helps, if you have more questions regarding this feel free to ask.