using System.Collections; using System.Collections.Generic; using UnityEngine; public class ggg : MonoBehaviour { // Start is called before the first frame update void Start() { CombineMesh(); } // Update is called once per frame void Update() { } void CombineMesh() { //获取所有子物体的网格 MeshFilter[] mfChildren = GetComponentsInChildren(); CombineInstance[] combine = new CombineInstance[mfChildren.Length]; //获取所有子物体的渲染器和材质 MeshRenderer[] mrChildren = GetComponentsInChildren(); Material[] materials = new Material[mrChildren.Length]; //生成新的渲染器和网格组件 MeshRenderer mrSelf = gameObject.AddComponent(); MeshFilter mfSelf = gameObject.AddComponent(); //合并子纹理 Texture2D[] textures = new Texture2D[mrChildren.Length]; Debug.Log(mrChildren.Length+"xxxxxxxxxxxxxxxxxxxxx"); for (int i = 0; i < mrChildren.Length; i++) { if (mrChildren[i].transform == transform) { continue; } materials[i] = mrChildren[i].sharedMaterial; Texture2D tx = materials[i].GetTexture("_MainTex") as Texture2D; Texture2D tx2D = new Texture2D(tx.width, tx.height, TextureFormat.ARGB32, false); tx2D.SetPixels(tx.GetPixels(0, 0, tx.width, tx.height)); tx2D.Apply(); textures[i] = tx2D; } //生成新的材质 Material materialNew = new Material(materials[0].shader); materialNew.CopyPropertiesFromMaterial(materials[0]); mrSelf.sharedMaterial = materialNew; //设置新材质的主纹理 Texture2D texture = new Texture2D(1024, 1024); materialNew.SetTexture("_MainTex", texture); Rect[] rects = texture.PackTextures(textures, 10, 1024); //根据纹理合并的信息刷新子网格UV for (int i = 0; i < mfChildren.Length; i++) { if (mfChildren[i].transform == transform) { continue; } Rect rect = rects[i]; Mesh meshCombine = mfChildren[i].mesh; Vector2[] uvs = new Vector2[meshCombine.uv.Length]; //把网格的uv根据贴图的rect刷一遍   for (int j = 0; j < uvs.Length; j++) { uvs[j].x = rect.x + meshCombine.uv[j].x * rect.width; uvs[j].y = rect.y + meshCombine.uv[j].y * rect.height; } meshCombine.uv = uvs; combine[i].mesh = meshCombine; combine[i].transform = mfChildren[i].transform.localToWorldMatrix; mfChildren[i].gameObject.SetActive(false); } //生成新的网格,赋值给新的网格渲染组件 Mesh newMesh = new Mesh(); newMesh.CombineMeshes(combine, true, true);//合并网格           mfSelf.mesh = newMesh; }  }