I have created a scene with terrain that is automatically divided into ‘hexes’ (which are actually handled as the point at the center, not the six sides). Here is what it looks like with my debug ‘golf balls’ turned on:

(For the record, the golf balls will have their mesh renderers removed for the final game).

The problem is the hexes are taking MUCH longer to load than I expected: my test scene (which has 10,000 hexes) takes several minutes to load, compared to a few seconds before the hexes were added.

Now I understand that 10,000 is a lot of hexes, but I would imagined it would go pretty quickly compared to computing each pixel of the terrain, and each leaf on each tree (there are a whole bunch of trees in that scene but not shown in the screenshot), and every thing the game does when I start.

Strangely, framerate appears to be relatively unaffected (even when showing the golf-balls); it’s just the loading of the hexes that takes forever.

Is there some way to make this process more efficient?

I am guessing it is the instantiating of the hexes that takes so long, is there a way to speed this up? Is there any OTHER way to speed this up?

Here is the relevent code:

``````void Build(){
hexHeight = hexSize * 2;
hexWidth = (float)((Math.Sqrt(3)/2) * hexHeight);
Hex[,] hexes = new Hex[numberOfRows, numberOfColumns];
if (myTerrain) {
if (shape == Shapes.Grid) {
//Build a grid of that is 'numberOfRows' deep
// and 'numberOfColumns' tall
for (int columnCounter = 0; columnCounter < numberOfColumns; columnCounter++) {
for (int rowCounter = 0; rowCounter < numberOfRows; rowCounter++) {
GameObject hexGameObject = (GameObject)(Instantiate(hexPrefab, new Vector3(0,0,0), Quaternion.identity)) as GameObject;
Hex newHex = hexGameObject.GetComponent<Hex>();
newHex.Initialize(this, columnCounter, rowCounter);
print("Creating a new hex- column:" + columnCounter + " row:"+rowCounter + " x:" + newHex.getX() + " y:" + newHex.getY() + " z:" + newHex.getZ());
//hexes[columnCounter,rowCounter] = newHex;
print("hexCount:" + ++hexCount);
}
}
}

}
else {
//todo there is no terrain, throw an error or something
}
}

public void Initialize(HexGrid hexGrid, int column, int row) {
this.hexGrid = hexGrid;
this.column = column;
this.row = row;
derivePosition();
transform.position = (new Vector3(x, y, z));
}

public void derivePosition() {
print ("size:" + hexGrid.hexSize + " column:" + column + " row:" + row);

x = (float)(column * hexGrid.HexWidth);
//every other column needs to be staggered
if (row % 2 == 0) { //if row is odd
x += hexGrid.HexWidth/2;
}
//this is the row number, multiplied by the height of a hex, multiplied by .75
z = (float)(row * hexGrid.HexHeight * .75);

if (hexGrid.myTerrain) {
//y = hexGrid.myTerrain.terrainData.GetHeight((int)(x), (int)(z));
y = Terrain.activeTerrain.SampleHeight(new Vector3(x, y, z));

}
}
``````

Remove the print statements, I bet that speeds it up quite a bit.