untiy 网格合并,材质球合并


前言

一个unity网格合并,同时支持贴图合并的笔记,总的是抄的别人的,添加了在unity编辑器模式下父物体合并子物体上的网格和贴图的功能,在编辑器下吧合并的网格赋值给Hierachy上物体,生成的材质球保存在Assets下.
这个算是我的笔记.

一、使用步骤

1.代码

1.创建一个MeshRenderEditor脚本,继承Editor.添加一个编辑器窗口入口,定义两个缓存路径.

 [MenuItem("MyTools/CombineMesh")]
    public static void EditorTest()
    {
        CombineMesh();
    }
    /// <summary>
    /// 图片缓存路径
    /// </summary>
    private static string JpgPath = "Assets/texture.jpg";
    /// <summary>
    /// material缓存路径
    /// </summary>
    private static string MaterialPath = "Assets/mat.mat";

2.合并网格和网格上贴图方法,可以把子物体上的网格合并成一个赋值给父物体.在运行模式下调用也会把合成的材质球和贴图放到父物体上.
如果不想在编辑模式下合并,在运行脚本里调用下面这方法,删除最后面的四个调用方法,也可以在运行模式下合并网格和贴图.

 static void CombineMesh()
    {
        #region 如果在游戏运行时动态合并网格,在脚本Start里运行这块代码就可以了
        GameObject gameObject = Selection.gameObjects[0];
        MeshFilter[] mfChildren = gameObject.GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[mfChildren.Length];

        MeshRenderer[] mrChildren = gameObject.GetComponentsInChildren<MeshRenderer>();
        Material[] materials = new Material[mrChildren.Length];

        MeshRenderer mrSelf = gameObject.AddComponent<MeshRenderer>();
        MeshFilter mfSelf = gameObject.AddComponent<MeshFilter>();

        Texture2D[] textures = new Texture2D[mrChildren.Length];
        for (int i = 0; i < mrChildren.Length; i++)
        {
            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);

        for (int i = 0; i < mfChildren.Length; i++)
        {
            if (mfChildren[i].transform == gameObject.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;
        #endregion 下面的代码是吧生成的Material,和合并的图片缓存在本地

      
        fileWriteTexture(texture, JpgPath);
        CreateMaterial(materialNew);
        LoadTextureAlter(JpgPath);
        MaterialSetTexture(MaterialPath, JpgPath);
       
    }

3.下面调用的四个方法,是把合成的材质球和贴图缓存在本地路径下.同事处理合成贴图的参数,绑定到材质球上

 /// <summary>
    /// 把合并好的图片缓存在"Assets/texture.jpg"路径下
    /// </summary>
    /// <param name="texture"></param>
    static void fileWriteTexture(Texture2D texture,string jpgPath)
    {
        var bytes = texture.EncodeToPNG();
        FileStream file = File.Open(jpgPath, FileMode.Create);
        BinaryWriter writer = new BinaryWriter(file);
        writer.Write(bytes);
        file.Close();
        texture.Apply();
    }
    /// <summary>
    /// 合并后的材质球缓存到MaterialPath路径下,刷新
    /// </summary>
    /// <param name="materialNew"></param>
    static void CreateMaterial(Material materialNew)
    {
        AssetDatabase.CreateAsset(materialNew, MaterialPath);
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 加载缓存合并图片,修改里面的参数
    /// </summary>
    static void LoadTextureAlter(string path)
    {
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
        textureImporter.textureType = TextureImporterType.Default;
        textureImporter.isReadable = true;
        AssetDatabase.ImportAsset(path);
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 缓存的Material存在上面贴图丢失的现象,重新绑定贴图数据
    /// </summary>
    static void MaterialSetTexture(string materialPath,string jpgPath)
    {
        Material LoadMaterial = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
        Texture2D Loadtextur = AssetDatabase.LoadAssetAtPath<Texture2D>(jpgPath);
        LoadMaterial.SetTexture("_MainTex", Loadtextur);
        AssetDatabase.Refresh();
    }

4.最后总的代码

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class MeshRenderEditor : Editor
{
    [MenuItem("MyTools/CombineMesh")]
    public static void EditorTest()
    {
        CombineMesh();
    }
    /// <summary>
    /// 图片缓存路径
    /// </summary>
    private static string JpgPath = "Assets/texture.jpg";
    /// <summary>
    /// material缓存路径
    /// </summary>
    private static string MaterialPath = "Assets/mat.mat";
    static void CombineMesh()
    {
        #region 如果在游戏运行时动态合并网格,在脚本Start里运行这块代码就可以了
        GameObject gameObject = Selection.gameObjects[0];
        MeshFilter[] mfChildren = gameObject.GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[mfChildren.Length];

        MeshRenderer[] mrChildren = gameObject.GetComponentsInChildren<MeshRenderer>();
        Material[] materials = new Material[mrChildren.Length];

        MeshRenderer mrSelf = gameObject.AddComponent<MeshRenderer>();
        MeshFilter mfSelf = gameObject.AddComponent<MeshFilter>();

        Texture2D[] textures = new Texture2D[mrChildren.Length];
        for (int i = 0; i < mrChildren.Length; i++)
        {
            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);

        for (int i = 0; i < mfChildren.Length; i++)
        {
            if (mfChildren[i].transform == gameObject.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;
        #endregion 下面的代码是吧生成的Material,和合并的图片缓存在本地

      
        fileWriteTexture(texture, JpgPath);
        CreateMaterial(materialNew);
        LoadTextureAlter(JpgPath);
        MaterialSetTexture(MaterialPath, JpgPath);
       
    }
    /// <summary>
    /// 把合并好的图片缓存在"Assets/texture.jpg"路径下
    /// </summary>
    /// <param name="texture"></param>
    static void fileWriteTexture(Texture2D texture,string jpgPath)
    {
        var bytes = texture.EncodeToPNG();
        FileStream file = File.Open(jpgPath, FileMode.Create);
        BinaryWriter writer = new BinaryWriter(file);
        writer.Write(bytes);
        file.Close();
        texture.Apply();
    }
    /// <summary>
    /// 合并后的材质球缓存到MaterialPath路径下,刷新
    /// </summary>
    /// <param name="materialNew"></param>
    static void CreateMaterial(Material materialNew)
    {
        AssetDatabase.CreateAsset(materialNew, MaterialPath);
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 加载缓存合并图片,修改里面的参数
    /// </summary>
    static void LoadTextureAlter(string path)
    {
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
        textureImporter.textureType = TextureImporterType.Default;
        textureImporter.isReadable = true;
        AssetDatabase.ImportAsset(path);
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 缓存的Material存在上面贴图丢失的现象,重新绑定贴图数据
    /// </summary>
    static void MaterialSetTexture(string materialPath,string jpgPath)
    {
        Material LoadMaterial = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
        Texture2D Loadtextur = AssetDatabase.LoadAssetAtPath<Texture2D>(jpgPath);
        LoadMaterial.SetTexture("_MainTex", Loadtextur);
        AssetDatabase.Refresh();
    }
}

总结

参考的大佬的博客,里面还有一个合并网格的方式,这个文章能合并的网格有限,如果是不同shader的材质球还需要处理,有问题可以看大佬的原博客.

https://blog.csdn.net/dardgen2015/article/details/51517860

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值