Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DavidN · May 03, 2016 at 08:36 AM · animationanimatorskinnedmeshrenderer

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
13
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePilgrim · Nov 12, 2018 at 07:08 PM 0
Share

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.

avatar image JannickL · Nov 29, 2018 at 04:31 PM 0
Share

Amazing! Thank you for this detailed answer.

avatar image MrPengin · Apr 30 at 06:26 PM 0
Share

Worked like a charm for attaching character customization option SkinnedMeshes to our rig! Thank you so much for sharing this!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges