Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Bunzaga · Dec 13, 2015 at 02:59 PM · animationscript.prefabsdynamic loadingskinned mesh

Scripted mesh combine with animation such as Armor, Clothing, etc.

Just to get everyone on the same page, I am not interested in an external bundle or plugin from the Asset Store to resolve this issue. I am not a fan of the 'answer your question with a purchase' mentality.

I have several skinned meshes which have a weight table, and are animated(created with 3ds Max). I have split these meshes up, so they could be re-combined through code, to create a variety of armor configurations.

Think of your typical RPG, where a user could have several options for helms, tunics, boots, pants, etc.

I've already found a solution to my issue, but I'm wanting to know if there is a BETTER solution. The way I have done this was tedious, and knowing Unity, there should be a built in way to do this without so much hassle on my end. I also wanted to post this, in case there are others out there, like myself, who wanted to do this, but couldn't find a way.

Here is a script I wrote to output the bones for the child mesh:

 using UnityEngine;
 
 public class DiscoverBones : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         Debug.Log ("**** Bone discovery ****");
         SkinnedMeshRenderer smr = gameObject.GetComponent<SkinnedMeshRenderer>();
         for (int i = 0; i < smr.bones.Length; i++) {
             Debug.Log ("Bone:"+i+"|"+smr.bones[i].name);
         }
         Debug.Log ("--- end ---");
     }
 }

This will output something like this:

**** Bone discovery ****

Bone:0|Bip01 L Clavicle

Bone:1|Bip01 Spine1

Bone:2|Bip01 Spine

Bone:3|Bip01 R Clavicle

Bone:4|Bip01 Pelvis

Bone:5|Bip01 L UpperArm

Bone:6|Bip01 R UpperArm

--- end ---

Once I record the bones, I remove the DiscoverBones script, then I can remove the mesh from the skeleton, add it as a prefab, and reference it with this script:

 using UnityEngine;
 
 public enum MeshSlot
 {
     Head = 1,
     Chest = 2,
     Arms = 4,
     Skirt = 8,
     SleeveCuff = 16,
     Hands = 32,
     GloveCuff = 64,
     Legs = 128,
     Shins = 256,
     Boots = 512,
     BootCuff = 1024
 };
 
 public class ArmorPiece : MonoBehaviour {
     
     public MeshSlot slotMask;
     public GameObject Prefab; // reference to the Child Mesh from above...
     public string[] Bones;
     public int RootBone;
     public Texture Texture;
 }

In this script, I take the bones listed in the DiscoverBones output, and put it in the Bones array. I also put in the RootBone.

So now I can call this function on the original parent object with the skeleton, passing in the prefab with the ArmorPiece attached to it:

 // root mesh GameObject of the original imported mesh (the one with the Animator)
 public Transform MyMesh; 
 public Transform Biped; // the biped GameObject imported
 
 private void CreateNewArmorMesh( ArmorPiece armorPiece){
     SkinnedMeshRenderer armorRef = armorPiece.Prefab.GetComponent<SkinnedMeshRenderer> ();
     var NewObj = new GameObject( armorRef.gameObject.name );
     NewObj.transform.parent = MyMesh.transform;
     var NewRenderer = NewObj.AddComponent<SkinnedMeshRenderer>();
 
     Transform[] boneMap = new Transform[armorRef.bones.Length];
 
     for( int i = 0; i < armorPiece.Bones.Length; i++){
         boneMap[i] = FindChildByName(armorPiece.Bones[i], Biped);
     }
 
     NewRenderer.bones = boneMap;
     NewRenderer.sharedMesh = armorRef.sharedMesh;
     NewRenderer.sharedMaterials = armorRef.sharedMaterials;
     NewRenderer.material.SetTexture ("_MainTex", armorPiece.Texture);
     NewRenderer.rootBone = boneMap[armorPiece.RootBone];
 }
 
 private Transform FindChildByName( string ThisName, Transform ThisGObj ){
     Transform ReturnObj = null;
     if (ThisGObj.name == ThisName) {
         return ThisGObj.transform;
     }
     foreach ( Transform child in ThisGObj )
     {
         ReturnObj = FindChildByName( ThisName, child );
         if( ReturnObj ){
             return ReturnObj;
         }
     }
     return null;
 }

As you can see, this is a real hassle, and not something easy to understand how to do if you're new to Unity. I figured there might be some setting or a way to reference the original mesh for the prefab. I just don't know enough about Unities animation and render system to know where to look.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can the animation editor create local rotational data? 3 Answers

What's the difference between animation and animator ? 0 Answers

how to switch 2d characters animation? 1 Answer

My animation is not playing 2 Answers

How do you increase an animations speed through c# 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