# How do you use submeshes correctly?

Hi!
I want to generate a tile map in unity and came up with two implementations:

1. using a gameobject for each tile
2. using MeshFilter, MeshRenderer: Create a submeshes for each tile

I expected that the version using a mesh would be faster than the one using gameobjects, but this was not the case - so I am wondering if I did it correctly (I was reading a tutorial where a big texture was generated by setting pixels and applied to the whole mesh - the author mentioned that using individual vertices for each tile would be more efficient and that it would be easy to apply different textures to each tile - I thought he meant submeshes):

I am generating my tiles using two loops:

``````Vector3[] vertices         = new Vector3[6*hexagonCount];
Vector2[] uv 	           = new Vector2[6*hexagonCount];
List<List<int>> triangles  = new List<List<int>>(hexagonCount);
for(int y=0; y<tileNumberY; ++y){
for(int x=0; x<tileNumberX; ++x){
generateHexMesh(ref vertices, ref uv, ref triangles, index, posX, posY, posZ);
}
}
``````

These loops fill my vertices, uv and triangles list/arrays. After that I do the following:

``````Material[] materials       = new Material[hexagonCount];
this.mFilter.mesh.vertices = vertices;  //add our vertices to the mesh
this.mFilter.mesh.uv       = uv; 	//add out UV coordinates to the mesh
this.mFilter.renderer.materials = materials;

for(int i=0; i<hexagonCount; ++i){
int[] temp = triangles*.ToArray();*
``````

this.mFilter.mesh.SetTriangles(temp, i);
this.mRenderer.materials = material;
}
this.mFilter.mesh.RecalculateBounds(); //recalcualte the dimensions of our mesh
this.mFilter.mesh.RecalculateNormals();
My code generates the desired Hexagons, but the framerate drops to 5 FPS when running it for 100x100 tiles, while the gameobject version runs at 300 FPS. Did I do something wrong or are submeshed not meant to be used in such ways? How are submeshes used correctly to be able to assign different textures to those tiles?
EDIT: Updated code according to robertbu’s suggestions
this.mFilter.mesh.vertices = vertices; //add our vertices to the mesh
this.mFilter.mesh.uv = uv; //add out UV coordinates to the mesh

for(int i=0; i<hexagonCount; ++i){
this.mFilter.mesh.SetTriangles(triangles*.ToArray(), i);*
}
this.mRenderer.material = material;
this.mFilter.mesh.RecalculateBounds(); //recalcualte the dimensions of our mesh
this.mFilter.mesh.RecalculateNormals();
UVS used for each hexagon:
// generate uv
Vector2[] uv = new Vector2[]{
new Vector2(0,0.25f),
new Vector2(0,0.75f),
*new Vector2(0.5f,1), *
new Vector2(1,0.75f),
new Vector2(1,0.25f),
*new Vector2(0.5f,0), *
};

The point of using submeshes is basically convenience, for example an animated character that contains several materials (skin, clothes, hair, eyes). The actual performance of submeshes is more or less the same as separate objects, so there’s generally no point using submeshes for tiles since you’re adding complexity for no gain. The two approaches to a tile system that make sense are 1) create meshes (typically without submeshes) which use a texture atlas and manipulate UVs to display the correct texture on each tile, or 2) create a pooling system where the tiles are separate objects, but only the visible tiles are actually created so as not to overwhelm Unity with umpteen thousand objects.

As of 2019/2020 … submeshes are still not fully implemented within Unity.

For instance, mesh.RecalculateBounds() was never updated to support them, so submeshes get broken lighting, reflection, etc (they are treated as if they are the size + shape of the total of all meshes).

Simply splitting your submeshes to separate objects instantly fixes a bunch of lighting problems.

TL;DR: as @Eric5h5 put it “basically convenience”, and in practice you should avoid them as much as possible. Too many partially-implemented parts (and given how many years it’s gone untouched, this isn’t going to be fixed).

for(int i=0; i<hexagonCount; ++i){
this.mFilter.mesh.SetTriangles(triangles*.ToArray(), i);*
}
This should be the culprit. You are creating hexagonCount instances of your mesh. Not a good idea.
Also remove the .ToArray(), it makes it much slower.
Use this:
Mesh mesh = new Mesh(); // fill it with your verts and normals
for(int i=0; i<hexagonCount; ++i){
mesh.SetTriangles(triangles*, i);*
}
this.mFilter.sharedMesh = mesh;
This should speed it up a lot, but I would still consider this bad practice. If you want to use several materials, you should group your submeshes and unite those that need the same material, however the good practice would be to create a texture atlas, adjust the UVs of each hexagon so it takes its information from the correct position of the atlas. This way you can render all your hexes with 1 drawcall and use only 1 submesh.
Also don’t use .material, it creates a new material everytime you do that. use .sharedMaterials instead