- Home /
Efficient grid system
I am asking this question here because i think i might be approaching this problem the wrong way. I am trying to program a grid system that will allow my players to design map layout similar to what you see in Dungeon Keeper or Evil Genius. I want the ability to load those layout and control their position in the scene, so i created a GridController object for that.
During Start the controller runs this code:
gridArray= new GameObject[gridSize,gridSize];
for (int i=0;i<gridSize;i++)
{
for (int n=0;n<gridSize;n++)
{
gridArray[i,n]=Instantiate(gridBlock,new Vector3(n,0,i),transform.rotation) as GameObject;
gridArray[i,n].transform.parent=transform;
}
}
gridBlock being the basic prefab later to be replaced by other prefabs through input, however even when that prefab is a basic block object the load time is very long and my pc slows down when i try to select several block through the scene camera. The current gridSize is 100.
Any lower grid size and my players won't have enough room to design layouts. Anyway to make this more efficient? Any ideas on a better approach?
100x100 is 10,000 prefabs. That's a lot of objects. You'll want to find a way to cull objects from your processing so you're never looping over the whole grid. What's slow? Rendering? Selection? Scrolling after selecting? How have you implemented selection?
In general, after your initialization in Start(), never loop over the entire grid, and try to avoid any heavy operations in the Update methods.
Also, consider using a pool manager. You don't want to be constantly initializing and destroying objects. This usually makes a big difference in projects with lots of dynamic objects such as projectiles that only "live" for a short time; it may or may not help you depending on your design.
Answer by Eric5h5 · May 07, 2013 at 02:00 PM
That would result in far too many GameObjects. Use the Mesh class instead to build chunks; see the various Minecraft topics, which generally have info about this sort of thing.