Im trying to implement a modular character system in Unity3d 5, which seems to be quite poorly documented. So far, what I have underestood from several sources is that I have to split the character mesh in the required parts, export each one separately including the skeleton, and export the skeleton without geometry. Then each part must be instantiated, but just to copy its Skinned Mesh Renderer component to a new GameObject, which is child of a parent game object that holds the asembled character mesh plus the skeleton. Also, each Skinned Mesh Renderer must have its bones overwriten with the ones from the skeleton.
I have tried to implement this, but I have only managed to make an script that runs and has no syntax errors, but it does nothing:
using UnityEngine;
using System.Collections;
public class assemble : MonoBehaviour {
Transform me;
Animator anim;
private Vector3 position;
NavMeshAgent agent;
GameObject head;
GameObject torso;
GameObject hands;
GameObject legs;
GameObject feet;
// Use this for initialization
void Start () {
me = transform;
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent> ();
GameObject prefab = Resources.Load("vmale-head") as GameObject;
GameObject tt2 = Instantiate (prefab);
head = new GameObject("head");
head.transform.SetParent( transform);
head.transform.localPosition = Vector3.zero;
head.transform.localRotation = Quaternion.Euler(Vector3.zero);
head.transform.localScale = transform.localScale;
var comp = head.AddComponent<SkinnedMeshRenderer>();
SkinnedMeshRenderer[] t = tt2.GetComponentsInChildren<SkinnedMeshRenderer> ();
if (t.Length > 0) {
comp.sharedMesh = t [0].sharedMesh; //copy geometry
comp.materials = t [0].materials; //and copy materials
} else
Debug.LogWarning("No components found!!");
Destroy (tt2);
prefab = Resources.Load("vmale-torso") as GameObject;
tt2 = Instantiate (prefab);
torso = new GameObject("torso");
torso.transform.SetParent( transform);
torso.transform.localPosition = Vector3.zero;
torso.transform.localRotation = Quaternion.Euler(Vector3.zero);
comp = torso.AddComponent<SkinnedMeshRenderer>();
t = tt2.GetComponentsInChildren<SkinnedMeshRenderer> ();
if (t.Length > 0) {
comp.sharedMesh = t [0].sharedMesh;
comp.materials = t [0].materials;
} else
Debug.LogWarning("No components found!!");
Destroy (tt2);
}
}
The parent gameobject gets the 2 children Im creating, but they are empty, no actual geometry is copied (or at least, it is not visible) and the new gameobjects are placed at 0,0,0, in the corner of the map. Also, I cant find a way to attach the skeleton to the parent or copy its bones, because the exported file doesnt produces an Skinned Mesh Render, as it has no geometry.
Can somebody give me some directions about this?