- Home /
How to vertex colour a single instance of a GameObject?
My question is what is the best way to handle fading of individual instance of GameObjects?
It is my understanding that when I instantiate a new clone of a GameObject Prefab - the sharedMesh property of the MeshFilter of all the instances will point to the same mesh, meaning that if I vertex colour that mesh then they will all fade together (for example) instead of individually as I'd like.
If, on the other hand I used the meshFilter.mesh property then it does work but every frame, for each GameObject I'm creating a new mesh and these don't seem to be well cleaned up. I.e. the number of meshes in my scene on the profiler goes through the roof and on IOS devices the game starts to stutter from garbage collection.
I was previously using the material colour to fade them, but that had the same problem - i.e. loads of materials being created etc... (and breaks batching)
I do have a solution that works, I just have the feeling there must be a better way. So, in a script, not attached to the GameObject, but containing that gameObject - so responsible for Instantiating it etc... I create a mesh. When I instantiate a gameObject there I copy its sharedMesh and then store a pointer to my mesh in the .sharedMesh property of the MeshFilter. And then i perform the vertex colouring on the sharedMesh. So there is only ever one per Instantiated GameObject.
I should say that there are lots of these GameObject's appearing and being destroyed all the time. Kindof Particly...
so, any hints on where i'm being particularly dim here?
Thanks :)
Have you tried forcing garbage collection at some frequency to smooth out the spikes?
if (Time.frameCount % 30 == 0) {
System.GC.Collect();
}
And you only changing the UVs of the existing mesh, not creating a whole new mesh each frame?
Answer by Bunny83 · Jul 08, 2013 at 12:14 AM
When accessing the mesh property Unity will only create a copy of the shared mesh when this instance doesn't have it's own version yet. Once it's created the MeshFilter is decoupled from the sharedMesh and is using it's own instance from now on.
Unity won't create a new instance each time you access the mesh property.
Yes, sorry you're absolutely right and this in fact makes sense with the numbers... i.e. the mesh count is increasing by hundreds, not hundreds of thousands as it would be if a new mesh was being created each frame per object and not destroyed - duh.
So it seems that my problem then is that i'm instantiated these objects at a rate of about 1 per frame and that for each one when i access the mesh property it creates a single new mesh and when i call GameObject.Destroy(..) later on the object, that mesh is not being cleaned up. (I.e. the count continues going upwards in the profiler... ...and related to this is the stuttering on IOS because that all disappears when i use the solution that works as i outlined above...
Right. I think it's pretty much the same with Renderer.material. Note the warning in the docs. I guess it's the same with the mesh property, so you have to destroy the mesh manually.