Why Unity freezes while creating Meshes procedurally?

I’m creating a mesh procedurally with around 10000 vertices, the problem is that when I start the mesh generation it freezes for a few moments, I used the time method to get the number of frames that past but it appears that not a single one past, then the mesh is finished, but I dont want that the program freeze each time that I generate a new mesh, 'cause I’m doing it constantly.

The arrays fills automatically before creating the mesh, but just filling them without sending them to the mesh there’s no lag, so I don’t think the problem is there.

Also, if ,after generating the mesh, a method move all the vertices around there’s no lag, so the manipulation of the mesh appears to be ok too, anyone has a clue of why is this happening?.

Here’s the codo so you can take a look:

	public void Build(	GameObject GObject,
						List<Vector3>	chunkPosition,
						List<Vector3>	chunkRotation,
						List<int>		chunkSides,
						List<int>		chunkSpace){
		float time1,time2,timeResult;
		time1 = Time.time;    		
		int	arraySize	  = 0,
			index		  =	0,
			sidesToRender = 0,
			tama		  =	chunkBlockID.Count;
		
		MeshFilter	meshFilter	= GObject.GetComponent<MeshFilter>();
		Mesh 		mesh		= meshFilter.mesh;

		mesh.Clear();
		
	//Each position in each list represents information of a cube that occupies that place
	//chunkSpace represent the number of triangles of the cube that are to be rendered

		for (int i = 0; i < tama; i++)
			arraySize += chunkSpace*;*
  •  Vector3[] vertices = new Vector3[arraySize];*
    
  •  Vector2[] uv       = new Vector2[arraySize];*
    
  •  int[] triangles    = new int[arraySize];*
    
  •  Vector3 Temp       = new Vector3(0,0,0);*
    
  •  ////ChunkSides contains six glags, one per side, so this cycle decides the orientation of each triangle*
    
  •  for (int i = 0; i < tama; i++){//Todos los posibles bloques*
    

_ sidesToRender = chunkSides*;_
_
for(int j = 0,selector = 1,Kstart = 0,Kend = 6;_
_
j < 6;_
_ j++ ,selector = 2,Kstart += 6,Kend += 6){_
_
//If the flag of a side is on, then add the respective rendering parts
_
* if((sidesToRender&selector) != 0)*
* for(int k = Kstart; k < Kend; k++, index++){*
* vertices[index] = Shapes.CubeVx[k]+Temp;*
* triangles[index]= index;*
* uv[index] = Shapes.CubeUV[k];*
* }*
* }*
* }*
* mesh.vertices = vertices;*
* mesh.triangles = triangles;*

* mesh.uv = uv;*
* mesh.RecalculateNormals();*
* mesh.RecalculateBounds();*
* mesh.Optimize();*
* time2 = Time.time;*
* timeResult = time2-time1;*
* print(timeResult);*
//Get the total time elapsed (it always gives me 0)
* }*

Do you use a MeshCollider? If so, that's your problem ;) Updating a MeshCollider is very (very) slow. <a href="https://dl.dropbox.com/u/7761356/UnityAnswers/Web/SphereTerrain/WebPlayer.html ">This</a> was an old webplayer i created. The cube is made up of 6 independent meshes each 128x128 vertices (as far as i remember). The recreation is very fast, but i don't use a MeshCollider. If i use one a single update might take up to 10 sec.

@Bunny83 - interesting about MeshColliders - I kind of never use them for other reasons - but now I've got a new shiny reason!!

That really helped, I went down from 25 miliseconds, to 8, in average!

I assume you've already realized this, but SystemInfo.supportsGyroscope returning false means that your hardware does not have a gyroscope to read from (or at least doesn't have one that Unity supports).

Building to pc did not work. building to UWP did. The hardware turned out to work, just not in the IDE. same issue as above. :(

1 Answer

1

Firstly no frames will pass if you are taking a long time to do something, it will be a synchronous operation.

If you want to know how long individual parts take you could use a StopWatch to time them.

Without seeing your code it’s hard to tell why its taking some time, but of course creating a mesh will potentially need to do lots of things with the GPU and allocate a significant number of objects. One solution would be to prepare a collection of meshes and then manipulate them as that seems to be a fast enough operation for you.

I just finished editing the question

Well it would certainly be worth working out which parts of that caused the delay using a StopWatch as I suggested. You can't rely on the Time functions as they are part of the game loop.

I used stop watch, the major problem was the collider.

Good to know!