Hello.
I am generating a hexagon mesh procedurally, and putting it in a game object, like so:
GameObject hex = new GameObject ();
hex.AddComponent<MeshRenderer> ();
hex.GetComponent<MeshRenderer> ().castShadows = false;
hex.GetComponent<MeshRenderer> ().receiveShadows = false;
hex.AddComponent<MeshFilter> ();
hex.GetComponent<MeshFilter> ().mesh = GenerateHexagonMesh ();
//hex.renderer.sharedMaterial = mat;
hex.isStatic = true;
I am instantiating the object about 400 times, without any performance issues.
However, if I set a material to the object (tested several), the performance drops to the point that Unity just stops responding. If I assign a material, only 10 or so objects can be instantiated before the performance gets too bad.
Any tips to what might be going wrong here?
Thanks
edit:
this is how i generate the mesh:
private Mesh GenerateHexagonalMesh (float hexRadius)
{
Mesh mesh = new Mesh ();
Vector3 vertPos = new Vector3 (0, 0, hexRadius);
Vector3[] vertices = new Vector3[7];
Quaternion rot = Quaternion.Euler (0, 60, 0); //360/6 = 60
vertices [0] = new Vector3 (0, 0, 0);
for (int i=0; i< 6; i++) {
vertPos = rot * vertPos;
vertices [i + 1] = vertPos;
}
mesh.vertices = vertices;
int[] tri = new int[]{
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 5,
0, 5, 6,
0,6,1};
mesh.triangles = tri;
Vector3[] normals = new Vector3[7];
normals [0] = Vector3.up;
normals [1] = Vector3.up;
normals [2] = Vector3.up;
normals [3] = Vector3.up;
normals [4] = Vector3.up;
normals [5] = Vector3.up;
normals [6] = Vector3.up;
mesh.normals = normals;
//mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
mesh.Optimize ();
return mesh;
}