Bone weight set/get is very slow??

I have a lot of meshes that need their bone weight offset in realtime (basically I wrote a skinned mesh combiner that adds geometry to their meshes, and appends bone weights accordingly).

The function runs extremely fast (under a millisecond) except for the part that accesses the mesh's bone weights. All it does is retrieve the original bone weight, offsets the value, and re-applies it. However, running that subroutine through each vertex takes over 30 milliseconds! (compared to under a millisecond to set all the verts, triangles, normals and uvs for the new mesh).

The pseudocode is as follow:

for (i = 0; i < mesh.vertices.length; i++)
{
   mesh.boneWeights*.boneIndex0 += offset;*
*}*
*```*
*<p>Any way to speed this up?</p>*

I think you need to access mesh.boneWeights as few times as possible. Try this simple performance test:

BoneWeight[] ws = new BoneWeight[mesh.boneWeights.Length];
for(int i = 0; i != ws.Length; ++i)
  ws*.boneIndex0 = i % numBones;*
*mesh.boneWeights = ws;*
*```*

I believe mesh.boneWeights *has to make a copy each time, so if you have 1000 vertices it will make 1000 copies. Try something like this and see if it works faster:

* *```* *BoneWeight[] ws = mesh.boneWeights;* *for (i = 0; i < mesh.vertices.length; i++)* *{* _ws*.boneIndex0 += offset;*_ _*}*_ _*mesh.boneWeights = ws;*_ _*```*_ _*

Anyway... modifying a mesh at runtime is not a good idea. Unity is not optimized for that.

*_