using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombinePlayer : MonoBehaviour
{
public List<GameObject> bodys=new List<GameObject>();
List<SkinnedMeshRenderer> skinneds = new List<SkinnedMeshRenderer>();
public int touid = 0;
public int tuiid = 0;
public int yifuid = 0;
// Start is called before the first frame update
void Start()
{
Change();
}
public void Change()
{
bodys.Add(Resources.Load<GameObject>("tou_" + touid));
bodys.Add(Resources.Load<GameObject>("tui_" + tuiid));
bodys.Add(Resources.Load<GameObject>("yifu_" + yifuid));
Transform[] allbones = GetComponentsInChildren<Transform>();
Dictionary<string, Transform> dicbones = new Dictionary<string, Transform>();
for (int i = 0; i < allbones.Length; i++)
{
dicbones.Add(allbones[i].name, allbones[i]);
}
List<Transform> bones = new List<Transform>();
List<CombineInstance> combines = new List<CombineInstance>();
List<Material> materials = new List<Material>();
for (int i = 0; i < bodys.Count; i++)
{
CombineInstance combine = new CombineInstance();
combine.mesh = bodys[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh;
//combine.transform = bodys[i].transform.localToWorldMatrix;//矩阵赋值
combines.Add(combine);
materials.Add(bodys[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial);
Transform[] bodybones = bodys[i].GetComponentInChildren<SkinnedMeshRenderer>().bones;
for (int j = 0; j < bodybones.Length; j++)
{
//if (dicbones.ContainsKey(bodybones[i].name))
//{
bones.Add(dicbones[bodybones[j].name]);
//}
}
}
Mesh mesh = new Mesh();
mesh.CombineMeshes(combines.ToArray(), false, false);
GetComponent<SkinnedMeshRenderer>().sharedMesh = mesh;
GetComponent<SkinnedMeshRenderer>().materials = materials.ToArray();
GetComponent<SkinnedMeshRenderer>().bones = bones.ToArray();
}
}
标签:换装,int,List,Add,new,bodys,public,3D From: https://www.cnblogs.com/Chenchen11111/p/17084809.html