//想要换的衣服放入数组
public GameObject[] arr;
void Start()
{
Mesh mesh = new Mesh();
List<CombineInstance> combines=new List<CombineInstance>();
List<Material> materials=new List<Material>();
List<Transform> bones=new List<Transform>();
// 找到我身上的所有骨骼 //骨骼是本体mesh网格是衣服
Transform[] mybones = GetComponentsInChildren<Transform>();
Dictionary<string,Transform> bonesdic=new Dictionary<string,Transform>();
//遍历我的骨骼 把他们放入字典中 名字作键 transform做值
foreach (var item in mybones)
{
bonesdic.Add(item.name, item);
}
for (int i = 0; i < arr.Length; i++)
{
materials.Add(arr[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial);
CombineInstance instance = new CombineInstance();
//获取子物体的mesh网格
instance.mesh = arr[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh;
//获取位置信息等
instance.transform = arr[i].transform.localToWorldMatrix;
combines.Add(instance);
//看看别人这件衣服是穿在什么位置,我也穿在这个位置
foreach (var item in arr[i].GetComponentInChildren<SkinnedMeshRenderer>().bones)
{
if(bonesdic.ContainsKey(item.name))
{
bones.Add(bonesdic[item.name]);
}
}
}
mesh.CombineMeshes(combines.ToArray(), false, false);
GetComponent<SkinnedMeshRenderer>().bones = bones.ToArray();
GetComponent<SkinnedMeshRenderer>().sharedMesh=mesh;
GetComponent<SkinnedMeshRenderer>().materials = materials.ToArray();
}
CombineInstance和SkinnedMeshRenderer实现 换装
最新推荐文章于 2024-08-31 14:48:17 发布