UNITY 网格合并

UNITY 网格合并

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

public class NewBehaviourScript : MonoBehaviour {

    public Avatar a;

	// Use this for initialization
	void Start () {


        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();

        Debug.Log("meshFilters:" + meshFilters.Length);

        CombineInstance[] combine = new CombineInstance[meshFilters.Length];



        MeshRenderer[] meshRendererList = GetComponentsInChildren<MeshRenderer>();  //获取自身和所有子物体中所有MeshRenderer组件
        Material[] mats = new Material[meshRendererList.Length];                    //新建材质球数组


        Texture2D[] texList = new Texture2D[meshRendererList.Length];


        int i = 0;
        while (i < meshFilters.Length)
        {

            //mats[meshFilters.Length-i-1] = meshRendererList[i].sharedMaterial;                           //获取材质球列表

            mats[i] = meshRendererList[i].sharedMaterial;

            //

            Color c = mats[i].color;

            /*
             * */

            Texture2D tx = mats[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();

            //
            texList[i] = tx2D;
            
            combine[i].mesh = meshFilters[i].sharedMesh;
            //combine[i].transform = meshFilters[i].transform.localToWorldMatrix;

            combine[i].transform = transform.worldToLocalMatrix * meshFilters[i].transform.localToWorldMatrix;

            meshFilters[i].gameObject.SetActive(false);
            i++;
        }

        //
        string shName = mats[0].shader.name;


        Material matNew = new Material(mats[0].shader);
        matNew.CopyPropertiesFromMaterial(mats[0]);


        Texture2D texNew = new Texture2D(1024, 1024);
        matNew.SetTexture("_MainTex", texNew);
        Rect[] rects = texNew.PackTextures(texList, 0);//, 2048);

        //
        GameObject quad = GameObject.Find("Quad");
        quad.GetComponent<MeshRenderer>().material.mainTexture = texNew;


        Mesh meshNew = new Mesh();

       
        //
        i = 0;
        while (i < meshFilters.Length)
        {
            int uvLen = combine[i].mesh.uv.Length;
            Vector2[] uvData = combine[i].mesh.uv;


            for(int j = 0;j< uvLen;j++)
            {
                uvData[j].x = uvData[j].x * rects[i].width + rects[i].x;
                uvData[j].y = uvData[j].y * rects[i].height + rects[i].y;


            }

            //直接设不行
            //combine[i].mesh.uv = uvData;

            meshFilters[i].mesh.uv = uvData;
            combine[i].mesh = meshFilters[i].mesh;

            i++;
        }

        

        transform.GetComponent<MeshFilter>().mesh = meshNew;

        transform.GetComponent<MeshFilter>().name = "xxx";

        transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, true);//,truefalse);////

        //为合并后的GameObject指定材质
        //transform.GetComponent<MeshRenderer>().sharedMaterials =mats;// materialNew;//          
        transform.GetComponent<MeshRenderer>().sharedMaterial = matNew;


        transform.gameObject.SetActive(true);


    }

    // Update is called once per frame
    void Update () {
		
	}
}

  

posted on 2017-04-16 10:17 jiahuafu 阅读(...) 评论(...) 编辑 收藏

Unity网格合并插件是一种用于合并游戏场景中多个网格模型的工具。在游戏开发中,为了提高性能和优化资源的使用,常常需要将多个小块的网格模型合并成一个大块的模型。 使用Unity网格合并插件可以简化这一过程,减少开发者的工作量。它提供了一个用户友好的界面,让开发者可以轻松地选择需要合并网格模型,并设置合并后模型的属性。 Unity网格合并插件可以帮助开发者解决以下问题: 1. 减少渲染调用:将多个小块的网格合并成一个大块的模型,可以减少渲染调用次数,提高渲染性能。 2. 优化资源使用:合并网格模型可以减少游戏运行时的内存消耗,提高游戏性能。 3. 简化碰撞检测:合并网格模型后,只需对一个大块的模型进行碰撞检测,可以优化游戏的物理运算。 使用Unity网格合并插件的步骤通常包括: 1. 导入网格模型:将需要合并网格模型导入到Unity中。 2. 创建合并对象:在Unity中创建一个新的空游戏对象作为合并后模型的容器。 3. 设置合并属性:选择需要合并网格模型,并设置合并后模型的属性,如材质、碰撞体等。 4. 运行合并操作:点击合并按钮,将选择的网格模型合并成一个大块的模型,并将其添加到合并对象中。 通过使用Unity网格合并插件,开发者可以有效地优化游戏的性能和资源的使用,提高游戏的流畅度和体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

银狐游戏开发资源2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值