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?