using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
//添加SkinnedMeshRenderer组件
[RequireComponent(typeof(SkinnedMeshRenderer))]
public class DrawMesh : MonoBehaviour
{
[SerializeField]
GameObject[] objs;
SkinnedMeshRenderer meshRenderer;
Mesh mesh;
// Start is called before the first frame update
void Start()
{
mesh=new Mesh();
meshRenderer=GetComponent<SkinnedMeshRenderer>();
SetSkinned();
}
//换装调用方法 传需要换的装备名字通过Resources替换集合 调用刷新方法
public void Reloading(Data data)
{
if(data.EquipType== 0)
{
objs[1] = Resources.Load<GameObject>(data.prefabs);
}
else if(data.EquipType == 1)
{
objs[2] = Resources.Load<GameObject>(data.prefabs);
}
else if(data.EquipType == 2)
{
objs[3] = Resources.Load<GameObject>(data.prefabs);
}
SetSkinned();
}
public void SetSkinned()
{
List<CombineInstance> instances = new List<CombineInstance>();
List<Material> materials = new List<Material>();
for (int i = 0; i < objs.Length; i++)
{
CombineInstance com = new CombineInstance();
com.mesh = objs[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh;
instances.Add(com);
Material mat = objs[i].GetComponentInChildren<SkinnedMeshRenderer> ().sharedMaterial;
materials.Add(mat);
}
mesh.CombineMeshes(instances.ToArray(),false,false);
meshRenderer.sharedMesh=mesh;
meshRenderer.materials=materials.ToArray();
Transform[] tran = GetComponentsInChildren<Transform>();
Dictionary<string,Transform> dic= new Dictionary<string,Transform>();
for (int i = 0; i < tran.Length; i++)
{
dic.Add(tran[i].name, tran[i]);
}
List<Transform> trans = new List<Transform>();
for (int i = 0; i < objs.Length; i++)
{
Transform[] arr = objs[i].GetComponentInChildren<SkinnedMeshRenderer>().bones;
foreach (var t in arr)
{
if(dic.ContainsKey(t.name))
{
trans.Add(dic[t.name]);
}
}
}
meshRenderer.bones = trans.ToArray();
}
}
人物换装 Mesh 骨骼 合并
最新推荐文章于 2024-11-12 10:17:03 发布