void addPart2() { var bones = getAllChildTransform(human.transform); List<Material> mats = new List<Material>(); List<Transform> newBones = new List<Transform>(); List<CombineInstance> combineInstances = new List<CombineInstance>();
foreach (var go in models) { var smrs = go.GetComponentsInChildren<SkinnedMeshRenderer>(); foreach (var smr in smrs) { foreach (var b1 in smr.bones) { Transform finded = null; foreach (var b in bones) if (b1.name == b.name) { finded = b; break; } if (finded != null) { newBones.Add(finded); } else { Debug.LogError("Can not find bone " + b1.name); } } mats.AddRange(smr.sharedMaterials);
for (int sub = 0; sub < smr.sharedMesh.subMeshCount; ++sub) { var ci = new CombineInstance(); ci.mesh = smr.sharedMesh; ci.subMeshIndex = sub; ci.transform = smr.transform.localToWorldMatrix; combineInstances.Add(ci); } } }
var humanSMR = human.AddComponent<SkinnedMeshRenderer>(); humanSMR.materials = mats.ToArray(); humanSMR.sharedMesh = new Mesh(); humanSMR.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false); humanSMR.sharedMesh.Optimize(); humanSMR.bones = newBones.ToArray(); }
void addPart3() { foreach (var go in models) { var smrs = go.GetComponentsInChildren<SkinnedMeshRenderer>(); foreach (var smr in smrs) { var originSmr = GameObject.Instantiate<SkinnedMeshRenderer>(smr); var bones = getAllChildTransform(human.transform);