- Home /
Make sure a mesh instance is not shared
I am using procedurally generated quad-meshes to display game sprites. I make changes to the panel sizes and uvs at run time in order to display sprite animations.
I want to make sure that each mesh instance is unique in order to avoid side effects when accessing one mesh instance in code. I have tried accessing the mesh using .mesh instead of .sharedMesh. This trick works but it displays errors in the editor warning me of memory leaks.
Answer by beartrickey · Aug 16, 2013 at 06:30 PM
I ran into the same problem, so I decided to just copy over the mesh data to a new mesh when I want to make changes to the quad. It stopped giving me the editor warnings.
var oldMesh : Mesh = target.gameObject.GetComponent( MeshFilter ).sharedMesh;
//make new instance of mesh
var newMesh : Mesh = new Mesh ();
newMesh.vertices = vertices;
newMesh.uv = oldMesh.uv;
newMesh.triangles = oldMesh.triangles;
target.gameObject.GetComponent( MeshFilter ).mesh = newMesh;
Well done, I've been looking for this workaround for sometime already. Thank you.
No problem! It's all a bit hacky of course ^_^;
That being said, I just bought 2D Tool $$anonymous$$it, which has solved my specific quad/sprite issues. On top of that, it looks like Unity 4.3 will offer some built-in 2D support, so you can save yourself $65 if you wait a bit :)
How did they solve the problem for the 2D Tool $$anonymous$$it? Do they copy the $$anonymous$$esh on Start/Awake?
Your answer
Follow this Question
Related Questions
Having trouble accessing a unique instance of a dynamically generated script... 0 Answers
How can I check if a GameObject has a Mesh? 1 Answer
Turn on mesh renderer when cam is looking. 1 Answer
Render VBO on GPU through MeshRenderer 1 Answer
Setting up a Skinned Mesh Renderer in Unity via Script 0 Answers