合并网格

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HB : MonoBehaviour
{

public GameObject[] objects;
// Start is called before the first frame update
void Start()
{
//创建合并实例的集合
List<CombineInstance> combines = new List<CombineInstance>();

//创建材质球集合
List<Material> materials = new List<Material>();
for (int i = 0; i < objects.Length; i++)
{
//创建合并实例
CombineInstance combine = new CombineInstance();
//获取网格并赋值给合并实例
combine.mesh = objects[i].GetComponent<MeshFilter>().mesh;//获得物体的网格
//获取矩阵并赋值给合并实例(transform是4*4矩阵类型)
combine.transform = objects[i].transform.localToWorldMatrix;//给矩阵赋值
//把合并实例存入集合
combines.Add(combine);
//获取材质球并存入材质球集合
materials.Add(objects[i].GetComponent<MeshRenderer>().material);

}

//创建网格
Mesh mesh = new Mesh();
//合并网格 (合并实例的数组,是否合并子网格,是否使用矩阵) //使用矩阵为false就不会和你摆的一样了
mesh.CombineMeshes(combines.ToArray(),false,false);//合并网格,后面加false图片就不会给子网格绘制
//合并后的网格赋值给网格过滤器
gameObject.AddComponent<MeshFilter>().mesh = mesh;
//把材质球集合赋值给材质球数组
gameObject.AddComponent<MeshRenderer>().materials = materials.ToArray();
}

// Update is called once per frame
 
}

 

posted @   天分丶张三  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示