void ChangePlayerBody()
{
Mesh mesh = new Mesh();
List<CombineInstance> combines = new List<CombineInstance>();
List<Material> materials = new List<Material>();
foreach (var item in bodys)
{
CombineInstance combine = new CombineInstance();
combine.mesh = item.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh;
combines.Add(combine);
materials.Add(item.GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial);
}
mesh.CombineMeshes(combines.ToArray(), false, false);
GetComponent<SkinnedMeshRenderer>().sharedMesh = mesh;
GetComponent<SkinnedMeshRenderer>().materials = materials.ToArray();
Transform[] allBones = GetComponentsInChildren<Transform>();
Dictionary<string, Transform> dicBones = new Dictionary<string, Transform>();
foreach (var item in allBones)
{
dicBones.Add(item.name, item);
}
List<Transform> mybones = new List<Transform>();
for (int i = 0; i < bodys.Length; i++)
{
Transform[] bodybones = bodys[i].GetComponentInChildren<SkinnedMeshRenderer>().bones;
for (int j = 0; j < bodybones.Length; j++)
{
if(dicBones.ContainsKey(bodybones[j].name))
{
mybones.Add(dicBones[bodybones[j].name]);
}
}
}
GetComponent<SkinnedMeshRenderer>().bones = mybones.ToArray();
}
一个数组存身体部位