In the game that I am making, a common task is to create new meshes to add to a pre-determined object. (The code for how I am currently doing this is included at the bottom of this post).
I create meshes by calculating the vertexes at 8 points on in the game, and generating a new temporary object mesh between those points (Parallelogram), then I create a new temporary empty gameObject, combine the master mesh with the temporary mesh in that object, then finally save the new temporary mesh back into the master mesh.
Obviously this is an extremely memory process, and when I test the game my FPS averages between 250 and 300, which drops to 25 while making objects (1 per frame, yes it is necessary), then immediately returns to its previous higher number when I stop creating. My question is: is there a more optimized process for creating meshes and adding them to an object?
Thank you all very much for tolerating my wordiness. I just thought more information would be better than less.
I don't have your project so I can't really pinpoint what is consuming most resources here. Some obvious ones are you're finding GameObjects several times, and looking up components several times. I rewrote your code (I hope it is still the same) to fix some obvious (potential) slowdowns, and renamed some variables to better reflect their purpose.
I know since before when I was working on some voxel system in Unity that assigning a mesh to a MeshCollider is a real pain performance wise. You'd have to profile your game to see exactly what calls here are too expensive. Also, you might sometimes be better off without Optimize() since it is quite heavy to perform.
With other words, it's not always a good idea to call it while the game is running.
answered Mar 11 '11 at 12:32 AM