I just recently figured this out. It’s semi-easy (conceptually).
Each bone in a mesh’s armature is just a transform.
Thusly: Take the bones of a shirt or whatever, and make them parented to the corresponding bones in the main body.
Little bit harder in practice to set it up right:
I wrote some scripts to help with this. They ONLY work if the parent and child mesh have the EXACT SAME ARMATURE. Otherwise they will break. Also, they must have the same ‘origin point’, ie, if they are dragged into the scene to the same spot, it would appear that they were parented.
Child objects should not have any animation. Only do animations on the parent.
So, ideally, to set up your models to work with this correctly:
Make a character in the ‘default clothing(underwear)’. Make sure you completely rig them and everything, so the only thing left for them is to be animated. After this point, you cannot under any circumstances change the rig.
Duplicate the file, for each clothing piece. In said files, replace the character’s mesh with the clothing. It kind of helps to keep the characters mesh if your program can copy mesh weighting information from one mesh to another, also so that you can also make sure the clothing completely covers the character, but don’t export the character mesh in to unity, just the clothing piece.
Animate the original character file, and only this file. You do not need to copy animations to other files/clothing pieces.
Keep an ‘unanimated’ copy somewhere on your drive if you’re thinking of keeping it to add more clothing pieces later. (cause you don’t want clothing pieces to have animations)
Bring everything in to unity, and make prefabs for each clothing piece, that includes the skinchild script on the root piece, with the variables as assigned. Remember, there is an ‘autopopulate’ function that you can run by clicking on the arrow for the script, and it will fill out the bone transform array. All of the bone transform arrays should turn out the exact same if you did everything correctly up to now. Make a prefab for the character model, also filling in the correct fields, but while using the skinparent script. It’s bone transform array should also be the exact same.
During runtime, if you want to instantiate a character with some specified clothing, merely spawn the character, the correct clothes, and then attach the skinparenter script to the character, and assign the values. Something like:
var skiPar : SkinParenter = parentObj.AddComponent(SkinParenter);
skiPar.parentM = parentObj.GetComponent(SkinParent);
skiPar.childM = new SkinChild[(number of clothing objects)];
skiPar.childM[0] = clothingObj1.GetComponent(SkinChild);
(etc for the rest of the clothes to fill the childM array)
. Before the frame is updated, the character and all the clothes should parent to each other, and in the correct position, as the Start function of the skinparenter parents all the clothing bones to their respective bones in the character’s armature.
Possible issues:
*Bone transform arrays are not the same after autopopulating: I doubt this will happen, but can be fixed manually if you still have the same number of bones. It’s just that each bone transform array on each piece needs to be the exact same as the skinparent’s bone array (order and everything).
*The character mesh seems start out in a different pose than the clothing, so the clothing gets parented in a weird spot: make sure that everything starts out in the exact same position/pose, including the character. It might be possible to fix this by changing a line in the skinparenter script to actually move each individual bone as it’s being parented to a 0 position and identity rotation, but I haven’t tried this out.
415640–14393–$SkinChild.js (618 Bytes)
415640–14394–$SkinParent.js (621 Bytes)
415640–14395–$SkinParenter.js (969 Bytes)