- Home /
Changing sharedMesh in MeshCollider causes lag
Hi,
I make changes to my mesh, and want to update the MeshCollider after I'm done. The problem is that it causes a lag of about 1-2 seconds. The whole program freezes for that time, I cannot move the camera, nothing.
I tried to circumvent this issue by putting the code for the mesh replacement in a Coroutine. But that doesn't have any effect.
If have no problem with the update taking 2 seconds. But I need to find a way to react to it (like displaying a text "Updating..."), and I still want to be able to move the camera and do other things while it is updating the mesh.
Here is the code snippet I'm using. The coroutine is called in Update when a mouse button is clicked and a raycast hits the object (that updates currentMeshFilter).
IEnumerator ApplyMeshColliderCoroutine(){
applyingMesh = true;
if(currentMeshFilter != null){
GameObject go = currentMeshFilter.gameObject;
MeshCollider mc = go.GetComponent<MeshCollider>();
mc.sharedMesh = null;
mc.sharedMesh = currentMeshFilter.sharedMesh;
}
currentMeshFilter = null;
applyingMesh = false;
yield return null;
}
I did some tests taking the time using System.DateTime
(because Time.time
didn't work), and what causes the lag is the line
mc.sharedMesh = currentMeshFilter.sharedMesh;
Any help greatly appreciated.
Answer by Tony-Sparrow · Nov 18, 2015 at 01:53 AM
I have same issue, and I find this
It is recommended to use this function only for reading mesh data and not for writing, since you might modify imported assets and all objects that use this mesh will be affected. Also, be aware that is not possible to undo the changes done to this mesh.
check out these link
http://answers.unity3d.com/questions/63313/difference-between-sharedmesh-and-mesh.html
http://docs.unity3d.com/ScriptReference/MeshFilter-sharedMesh.html
Your answer
Follow this Question
Related Questions
Mesh Collider with model that uses armature. 0 Answers
Does collider size affect performance? 1 Answer
Replace MeshFilter mesh by a other mesh in Editor 1 Answer
difference between sharedMesh and Mesh 1 Answer
Finding sharedMesh users. 0 Answers