- Home /
Sharing a generated mesh between multiple game objects
So I have a component script, "GeneratedMesh", which I am attaching to a prefab. Basically, I would like every instance of a prefab to use the same generated mesh, but as I have it right now, the mesh is regenerated for every instance. So it looks something like this right now:
MyPrefabA has GeneratedMesh script
MyPrefabB has GeneratedMesh script
Instantiate MyPrefabA, MyPrefabA, MyPrefabB, MyPrefabB.
What happens:
GeneratedMesh generates 4 times, once for each instance.
What I would like to happen:
GeneratedMesh generates a mesh for MyPrefabA, then the second time it realizes MyPrefabA already has a mesh generated, and reuses it for the second instance. Then it generates a mesh for the first instance of MyPrefabB, then for the second MyPrefabB, it realizes there is already a mesh generated and uses that.
In the end, it only generates 2 times.
Leads I have so far:
I notice that MeshFilter has a 'sharedMesh' property. Presumably if I set the sharedMesh of the second instance to be the same as the first, Unity will consider them identical geometry and handle batching and all that.
So what I really need are suggestions for determining which prefab a mesh was generated for (really, it's all based on the parameters of the GeneratedMesh script on the prefab, as well as the material on the prefab), and then using that to coordinate sharing the mesh between instances...
Any thoughts?
Answer by DaveA · Jan 10, 2011 at 10:52 PM
Could you put the mesh generator on a different object, of which there is only one, so your script runs once. Then when you instantiate more objects, Find (or keep a reference to) that first object, then assign it's mesh out of there?
Hmmm, yes, I should have thought of that. ;) The wheels are turning, I'll check back and let you know what I ended up with.
This ended up being a working solution. I have a static '$$anonymous$$eshes' class that keeps a canonical reference to the generated mesh, and all the other objects which need the mesh reference it here.
Answer by Jessy · Jan 10, 2011 at 11:42 PM
It's tough to tell, without code. But it's sounds like you're onto the solution. If you keep a reference to a mesh you created, then you can assign that to as many MeshFilter.mesh-es or MeshFilter.sharedMesh-es as you want, and the result will be the same: each MeshFilter will use the same (instanced one time only) mesh. However, if you ever get a mesh reference using a MeshFilter.mesh, then that mesh will be instanced.
Also, be aware that MeshFilter.mesh is only designed for runtime use; MeshFilter.sharedMesh is what you need to use in the Editor. This is a nice indicator of good practice: if your mesh generation scripting doesn't require multiple instances, run your code in the Editor. If you don't see errors, you're probably making nice, efficient code for runtime use.
This sounds like exactly what I want. I'm capable of searching of course, but if you have a quick summary of what "Run your code in the Editor" means, I'd appreciate it. :)
Sure: http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$enuItem.html http://unity3d.com/support/documentation/ScriptReference/Editor.html
I tend to run as little setup code as possible, in-game. I use a fair amount of procedural meshes, but I generally don't have to wait for runtime to create them. Of course this can lear to file size increase, but if the meshes are small, they're probably smaller than the code itself. Here's an example: http://forum.unity3d.com/threads/58536-reveal-a-texture?p=375739&viewfull=1#post375739
"lead", not "lear". $#%^*&@ I wish you could just edit comments!!! :-P
Your answer
Follow this Question
Related Questions
Generated meshes/materials cannot be made into prefabs? 1 Answer
Mesh memory leak error 0 Answers
difference between sharedMesh and Mesh 1 Answer
Mesh Filters randomly self deleted 0 Answers