效果如下:
我们可以在主界面去修改选渲染的各种部位,也可以使用代码进行动态更换。
代码如下:
using Newtonsoft.Json.Bson;
using System.Collections;
using System.Collections.Generic;
using TMPro.EditorUtilities;
using UnityEngine;
public class HuanOne : MonoBehaviour
{
public GameObject[] bon;
public static HuanOne ins=null;
private void Awake()
{
if (ins == null)
{
ins=this;
}
}
// Start is called before the first frame update
void Start()
{
}
public void GetBod(int type,string name)
{
bon[type]=Resources.Load<GameObject>(name);
}
public void Onpl(GameObject[] bon)
{
List<CombineInstance> com = new List<CombineInstance>();
List<Material> mat = new List<Material>();
List<Transform> trans = new List<Transform>();
Transform[] all=this.GetComponentsInChildren<Transform>();
Dictionary<string,Transform> dic = new Dictionary<string,Transform>();
foreach (var t in all)
{
dic.Add(t.name, t);
}
for(int i=0;i<bon.Length;i++)
{
CombineInstance combine = new CombineInstance();
combine.mesh = bon[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh;
com.Add(combine);
mat.Add(bon[i].GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial);
Transform[] btn = bon[i].GetComponentInChildren<SkinnedMeshRenderer>().bones;
for(int j = 0; j < bon.Length; j++)
{
if (dic.ContainsKey(btn[j].name))
{
trans.Add(dic[btn[j].name]);
}
}
}
Mesh mesh= new Mesh();
mesh.CombineMeshes(com.ToArray(), false, false);
this.GetComponent<SkinnedMeshRenderer>().sharedMesh= mesh;
this.GetComponent<SkinnedMeshRenderer>().materials=mat.ToArray();
this.GetComponent<SkinnedMeshRenderer>().bones=trans.ToArray();
}
// Update is called once per frame
void Update()
{
}
}