Attach more than one skinned meshrenderer to same animator.
I want a good way to attach more than one skinned meshrenderer to the same animator for character customisation. Right now the we are running one animator for each bodypart and this sometimes make the animators go out of sync.
Answer by incorrect · May 03, 2016 at 01:14 PM
I've solved this recently for myself, so here is how I do it.
To make skinned meshes share rig and animations, you need to attach skinned meshes to this rig. Unfortunately, you can't assign skinned mesh's bones through the inspector. Actually, bones are stored in an array of SkinnedMeshRenderer called bones. The order in which they are stored is critical.
So I managed to store names of the bones and after attaching mesh to the rig I'm looking for same bones in it. Keep in mind that models have to be made with the same rigs with same names.
Here is a little extension method helping with deep search in rig to find bones:
using UnityEngine;
using System.Collections;
public static class ExtensionMethods
{
private static Transform _findInChildren(Transform trans, string name)
{
if(trans.name == name)
return trans;
else
{
Transform found;
for(int i = 0; i < trans.childCount; i++)
{
found = _findInChildren(trans.GetChild(i), name);
if(found != null)
return found;
}
return null;
}
}
public static Transform FindInChildren(this Transform trans, string name)
{
return _findInChildren(trans, name);
}
}
And here is a script that binds a model to the rig.
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class BoneSetter : MonoBehaviour
{
[SerializeField]
private Transform skeletonRoot;
[SerializeField]
private string[] boneNames;
[SerializeField]
private Transform[] bones;
[ContextMenu("GetBoneNames()")]
public void GetBoneNames()
{
SkinnedMeshRenderer srenderer = GetComponent<SkinnedMeshRenderer>();
if(srenderer == null)
return;
Transform[] bones = srenderer.bones;
boneNames = new string[bones.Length];
for(int i = 0; i < bones.Length; i++)
boneNames[i] = bones[i].name;
}
[ContextMenu("SetBones()")]
public void SetBones()
{
if(skeletonRoot == null)
{
Debug.LogError("Root object is not set!");
return;
}
bones = new Transform[boneNames.Length];
for(int i = 0; i < boneNames.Length; i++)
{
bones[i] = skeletonRoot.FindInChildren(boneNames[i]);
}
SkinnedMeshRenderer srenderer = GetComponent<SkinnedMeshRenderer>();
srenderer.bones = bones;
srenderer.rootBone = skeletonRoot;
}
void Awake()
{
SetBones();
}
}
So to make it work, I'm putting a mesh that I want to attach in a scene. It will be instantiated with it's own rig. After that I attach my BoneSetter script to the object with SkinnedMeshRenderer. Right clicking on a BoneSetter script will open a context menu where you can find my custom option GetBoneNames(). It will save bone names in an array that you can view through inspector just to make sure everything worked as expected. Those are bones that are used to animate this particular mesh in order they are used.
After that you can detach your mesh from it's original object and attach it to the rig you want to put it on.
Then you need to set a field skeletonRoot of the BoneSetter via inspector placing there your rigs root bone. It will be used to search for bones with names stored in an array of the script.
Once it is done, you can manually call bone setting right clicking on a script and choosing SetBones() or it will be automatically called in Awake().
If you will do it manually, you can remove BoneSetter from a mesh: now bone array will be stored in a mesh renderer and serialized by Unity, though it is not shown in inspector.
I'm still improving my solution, but it already works. Feel free to ask if you have any questions left.
Thanks for sharing your solution. I had the same requirement for a customizable character model made up of multiple different mesh renderers and this helped me greatly.
Worked like a charm for attaching character customization option SkinnedMeshes to our rig! Thank you so much for sharing this!
Your answer
Follow this Question
Related Questions
runtime Animator BlendShapes not working 0 Answers
my animation glitches when i add more keyframes 0 Answers
How to make arms aim at target 0 Answers
Kick bool being blocked by Ground bool 0 Answers
Increase attack speed and animations 1 Answer