- Home /
Need help optimizing grid initialization.
Hello everyone,
I am working on a game which uses a 40 X 40 grid. Each cell in the grid is made up of GridCell objects.
class GridCell
{
public Vector3 cell_pos;
public bool cell_status;
public GameObject cell_go;
}
And here is how I am initializing the grid currently,
void CreateGrid() { _grid = new GridCell[Config.GRID_ROW_COUNT, Config.GRID_COL_COUNT];
for (int i = 0; i < Config.GRID_ROW_COUNT; ++i)
{
for (int j = 0; j < Config.GRID_COL_COUNT; ++j)
{
_grid[i, j] = new GridCell();
_grid[i, j].cell_pos = new Vector3(Config.GRID_CELL_WIDTH / 2 + i * Config.GRID_CELL_WIDTH, 0.05f, (Config.GRID_CELL_HEIGHT / 2 + j * Config.GRID_CELL_HEIGHT));
_grid[i, j].cell_status = false;
_grid[i, j].cell_go = Instantiate(Resources.Load("GridCell")) as GameObject;
_grid[i, j].cell_go.renderer.enabled = false;
_grid[i, j].cell_go.transform.position = _grid[i, j].cell_pos;
}
}
}
As you can see I am initializing 1600 GameObjects here and this causes my load time to shoot up. Is there anyway I can optimize this initialization. It will be really helpful.
Thank you!
Answer by cncguy · Aug 06, 2010 at 12:55 AM
I had this same issue. As far as I could see if you have to create a gameobject then you have to create a gameobject and suffer the consequences. All I have to suggest is putting a yield statement in the inner or outer loop. This will stop your game 'freezing' as it loads but will have the appearance of creating the grid over a number of frames. You could however make this a 'feature' Of the start of the level to mitigate the issue. I.e. A sort of materialization of your grid fanning out from some point.
[edit] you are calling Resources.load every object. Take that out of the loop and create a reference to it. Then instantiate via the reference.Also use the full constructor for instantiate to save a call to transform every iteration. Eg. Something like:
void CreateGrid() { _grid = new GridCell[Config.GRID_ROW_COUNT, Config.GRID_COL_COUNT];
GameObject gridObj =Resources.Load("GridCell")) as GameObject;
for (int i = 0; i < Config.GRID_ROW_COUNT; ++i) { for (int j = 0; j < Config.GRID_COL_COUNT; ++j) { _grid[i, j] = new GridCell(); _grid[i, j].cell_pos = new Vector3(Config.GRID_CELL_WIDTH / 2 + i Config.GRID_CELL_WIDTH, 0.05f, (Config.GRID_CELL_HEIGHT / 2 + j Config.GRID_CELL_HEIGHT)); _grid[i, j].cell_status = false; _grid[i, j].cell_go = Instantiate(gridObj,_grid[i, j].cell_pos, Quaternion.identity)as GameObject; _grid[i, j].cell_go.renderer.enabled = false; } } }
sorry about the formatting - tricky on the iPhone. Ps I code in jscript so may not be 100% syntactically correct.
[edit 2]
make sure your grid has it's renderer enabled by default and remove the call that enables the rendere for each cell.
Thanks a lot cncguy! Really appreciate it. Though there was no significant performance improvement (and I understand that is the drawback with initializing so many GameObjects) I like your idea to mitigate the problem. Add some flashy effects and I think I can get away with it :) Thanks again!