Unity场景网格合并

网格合并有两种方式以及区别

效果都是把使用相同材质球的mesh进行合并

静态就是预先处理会增加包体大小,看需求,如果减包体可以考虑使用动态合并

 

1.勾选静态精选合并

所有被勾选了“Static”的GameObject,其中的Mesh Filter中的mesh都会被合并到 "Combined Mesha (root: scene)" 中

 

2.使用脚本动态合并

        //找到需要合并的节点
        GameObject staticObjects = GameObject.Find("Static");
        if (null == staticObjects)
            return;

        //遍历所有render,把材质球对应的render列表存起来
        Dictionary> Material2RendersDic = new Dictionary>();
        Renderer[] allRenders = staticObjects.GetComponentsInChildren();
        for (int i = 0; i < allRenders.Length; i++)
        {
            Material mat = allRenders[i].sharedMaterial;
            if (null == mat)
                continue;
            if (!Material2RendersDic.ContainsKey(mat))
                Material2RendersDic.Add(mat, new List());
            Material2RendersDic[mat].Add(allRenders[i]);
        }

        //根据相同材质球的mesh进行合并
        foreach (KeyValuePair> mat in Material2RendersDic)
        {
            List RenderList = mat.Value;
            if (RenderList.Count <= 1)
                continue;
            GameObject[] objs = new GameObject[RenderList.Count];
            for (int j = 0; j < mat.Value.Count; j++)
                objs[j] = RenderList[j].gameObject;
            StaticBatchingUtility.Combine(objs, objs[0]);
        }

 

你可能感兴趣的:(性能优化)