I have code wherein I have this one function where I create a big mesh, adding 6,400 vertices, which is really lagging the program. But once its done, the program runs smoothly, so I thought of putting it in a separate thread. Here's the code: Code (csharp): function Start() { var createThread = new System.Threading.Thread( new System.Threading.ThreadStart(CreateFogOfWarMesh) ); createThread.Start(); } CreateFogOfWarMesh is the function in question. Code (csharp): function CreateFogOfWarMesh() { gameObject.AddComponent("MeshFilter"); gameObject.AddComponent("MeshRenderer"); gameObject.AddComponent("MeshCollider"); mesh = GetComponent(MeshFilter).mesh; mesh.Clear(); // vertices --------------------------------------------------------------------------------- var verts = new Array(); var pos = transform.position; //var locZ = transform.position.z; for (var y = 0; y < yLength; ++y) { for (var x = 0; x < xLength; ++x) { verts.Push( AddVert(pos,(x*gridSize),(y*gridSize)) ); } } mesh.vertices = verts.ToBuiltin(Vector3); // uv's ---------------------------------------------------------------------------------------- var uvs = new Vector2[verts.length]; for (var i=0;i<uvs.Length;i++) { uvs[i] = Vector2(verts[i].x, verts[i].z); } mesh.uv = uvs; // triangles --------------------------------------------------------------------------------- var tris = new Array(); //var trilen = (mesh.vertices.length-2)/2; for (y = 0; y < yLength-1; ++y) { for (x = 0; x < xLength-1; ++x) { var vertHere = x+(y*xLength); var vertAbove = x+((y+1)*xLength); tris.Push(vertHere); tris.Push(vertAbove); tris.Push(vertAbove+1); tris.Push(vertHere); tris.Push(vertAbove+1); tris.Push(vertHere+1); //Debug.Log(x + "," + y + ": " + // vertHere + "," + vertAbove + "," + (vertAbove+1) + " " + // vertHere + "," + (vertAbove+1) + "," + (vertHere+1)); } } mesh.triangles = tris.ToBuiltin(int); mesh.RecalculateNormals(); mesh.RecalculateBounds(); // vertex colors ------------------------------------------------------------------------------ var vertices = mesh.vertices; colors = new Color[vertices.Length]; for (i=0;i<vertices.Length;i++) { colors[i] = Color(0,0,0,targetAlpha); } mesh.colors = colors; renderer.castShadows = false; renderer.receiveShadows = false; renderer.material = newMaterial; var meshCollider : MeshCollider = GetComponent(MeshCollider); meshCollider.sharedMesh = mesh; creationDone = true; } The only problem is its spewing out lots of cryptic error messages upon starting, and there's a chance Unity will hang and I have to forcefully end its process. Any help is appreciated.
Ok, moving the gameObject.AddComponent function outside the thread lessened the amount of errors. Code (csharp): function Start() { gameObject.AddComponent("MeshFilter"); gameObject.AddComponent("MeshRenderer"); gameObject.AddComponent("MeshCollider"); var createThread = new System.Threading.Thread( CreateFogOfWarMesh ); createThread.Start(); //CreateFogOfWarMesh(); } Weird thing is, I'm still getting a noticeable lag in my program even though its threaded already.
you can not access any UnityEngine classes and functionality from within distinct threads. you can only work with the threadsafe System classes
Right you can't, thats why you normally do it as part of the loading after the actual scene is loaded. you just keep the loading screen there and do the initialization behind it for example. In this case though there would be ways to utilize threading as array etc are valid classes. so you can prepare these data. Question though is how much faster it would be if you stopped the dynamic expansion approach caused by Push.
The loading screen will become unresponsive though, I was hoping to multi-thread it so at least the loading screen can animate its progress bar without hiccups. The lag isn't coming from the arrays. Its when the actual mesh is created. When I removed the code that creates the mesh but left the code for the arrays, there was no lag.