예제 #1
0
    private static GameObject AddMeshToNewGameObject(Mesh[] meshes, bool addMeshCollider = false, string name = null)
    {
        //Set up our gameobject and add a renderer and filter
        GameObject obj = new GameObject();

        obj.AddComponent <MeshRenderer>();
        obj.AddComponent <MeshFilter>();

        Renderer objRenderer = obj.GetComponent <Renderer>();

        Material[]      mats       = new Material[meshes.Length];
        MaterialHandler mtlHandler = MaterialHandler.GetInstance();

        for (int i = 0; i < meshes.Length; i++)
        {
            Material mat = mtlHandler.GetMaterialForMesh(meshes[i].name);

            if (mat == null)
            {
                Debug.LogFormat("Could not find material for mesh {0}", meshes[i].name);
                mats[i] = mtlHandler.GetMaterialForMesh("default");
            }
            else
            {
                mats[i] = mat;
            }
        }
        objRenderer.materials = mats;

        Mesh main = new Mesh();

        main.subMeshCount = meshes.Length;

        if (meshes.Length == 0)
        {
            obj.GetComponent <MeshFilter>().mesh = main;
        }
        else
        {
            CombineInstance[] combine = new CombineInstance[meshes.Length];

            for (int i = 0; i < meshes.Length; i++)
            {
                combine[i].mesh      = meshes[i];
                combine[i].transform = Matrix4x4.identity;
            }
            main.CombineMeshes(combine);

            for (int i = 0; i < main.subMeshCount; i++)
            {
                int[] tri = meshes[i].triangles;

                int offset = 0;
                if (i > 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        offset += meshes[j].vertexCount;
                    }
                }

                main.SetTriangles(tri, i, false, offset);

                //Don't ask?
                if (main.subMeshCount != meshes.Length)
                {
                    main.subMeshCount = meshes.Length;
                }
            }

            obj.GetComponent <MeshFilter>().mesh = main;
        }
        if (addMeshCollider)
        {
            obj.AddComponent <MeshCollider>();
        }

        string newName = "";

        //Redo this
        if (name != null)
        {
            newName = name;
        }
        else
        {
            newName = meshes.Length > 0 ? meshes[0].name.Substring(0, meshes[0].name.Length - 2) : "Unknown";
        }

        obj.name = newName;
        obj.SetActive(false);
        return(obj);
    }
예제 #2
0
    private static void AddMeshToGameObject(Mesh[] meshes, GameObject obj)
    {
        Renderer objRenderer = obj.GetComponent <Renderer>();

        Material[]      mats       = new Material[meshes.Length];
        MaterialHandler mtlHandler = MaterialHandler.GetInstance();

        for (int i = 0; i < meshes.Length; i++)
        {
            Material mat = mtlHandler.GetMaterialForMesh(meshes[i].name);

            if (mat == null)
            {
                Debug.LogFormat("Could not find material for mesh {0}", meshes[i].name);
                mats[i] = mtlHandler.GetMaterialForMesh("default");
            }
            else
            {
                mats[i] = mat;
            }
        }
        objRenderer.materials = mats;

        Mesh main = new Mesh();

        main.subMeshCount = meshes.Length;

        if (meshes.Length == 0)
        {
            obj.GetComponent <MeshFilter>().mesh = main;
        }
        else
        {
            CombineInstance[] combine = new CombineInstance[meshes.Length];

            for (int i = 0; i < meshes.Length; i++)
            {
                combine[i].mesh      = meshes[i];
                combine[i].transform = Matrix4x4.identity;
            }
            main.CombineMeshes(combine);

            for (int i = 0; i < main.subMeshCount; i++)
            {
                int[] tri = new int[0];

                tri = meshes[i].triangles;

                int offset = 0;
                if (i > 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        offset += meshes[j].vertexCount;
                    }
                }

                main.SetTriangles(tri, i, false, offset);

                //Don't ask?
                if (main.subMeshCount != meshes.Length)
                {
                    main.subMeshCount = meshes.Length;
                }
            }

            obj.GetComponent <MeshFilter>().mesh = main;
        }
    }