Combining submeshes, only one submesh renders if CombineMeshes is set to false.

Hey All!

A friend and i recently made a modular weapon pack for the UE4 marketplace and we’re looking to convert it to unity as well. Progress is going well, but i’m stuck on an issue here.

Below is the function used to generate the objects mesh components from a string.

public void SetMeshComponents()
    {
        List<string> partCodes = GetPartsFromCode();
        List<GameObject> meshComponents = new List<GameObject>();
        for( int i = 0; i < partCodes.Count; ++i )
        {
            for( int j = 0; j < partCodes.Count; ++j )
            {
                if( partCodes[i].StartsWith( WeaponTypes[j].partPrefix ) )
                {
                    string[] meshIDText = partCodes[i].Split( WeaponTypes[j].partPrefix.ToCharArray() );
                    int meshID = S_MWeapon.IntParseFast( meshIDText[1] );
                    meshComponents.Add( WeaponTypes[j].meshComponent[meshID] );
                }
            }
        }
       
        CombineInstance[] combine = new CombineInstance[meshComponents.Count];
        int k = 0;
        while( k < meshComponents.Count )
        {
            combine[k].mesh = meshComponents[k].GetComponent<MeshFilter>().sharedMesh;
            combine[k].transform = meshComponents[k].GetComponent<MeshFilter>().transform.localToWorldMatrix;
            meshComponents[k].GetComponent<MeshFilter>().gameObject.SetActive( false );;
            k++;
        }
        transform.GetComponent<MeshFilter>().mesh = new Mesh();
        transform.GetComponent<MeshFilter>().sharedMesh.subMeshCount = meshComponents.Count;
        transform.GetComponent<MeshFilter>().sharedMesh.CombineMeshes( combine, false );
        transform.gameObject.SetActive( true );
    }

In particular, this code is the problem:

transform.GetComponent<MeshFilter>().sharedMesh.CombineMeshes( combine, false );

If i remove the “false” argument, the submeshes are combined into a single mesh. if not, then only the first submesh is rendered. I need all submeshes to render and i need them all to be separate submeshes so that i can assign a material to each of them.

Any ideas would be gratefully received.

Fixed it.

The problem was that there was no mesh renderer being carried over for each mesh, so it only rendered the first mesh. this is the fixed code (for any else who have trouble, it’s not complete though, it’s not correctly applying materials yet for whatever reason).

void SetMeshComponents()
    {
        List<string> partCodes = GetPartsFromCode();
        List<GameObject> meshComponents = new List<GameObject>();
        List<Material> meshMats = new List<Material>();
        for( int i = 0; i < partCodes.Count; ++i )
        {
            for( int j = 0; j < partCodes.Count; ++j )
            {
                if( partCodes[i].StartsWith( WeaponTypes[j].partPrefix ) )
                {
                    string[] meshIDText = partCodes[i].Split( WeaponTypes[j].partPrefix.ToCharArray() );
                    int meshID = S_MWeapon.IntParseFast( meshIDText[1] );
                    meshComponents.Add( WeaponTypes[j].meshComponent[meshID] );
                    meshMats.Add( WeaponTypes[j].meshComponent[meshID].GetComponent<MeshRenderer>().sharedMaterial );
                }
            }
        }
       
        CombineInstance[] combine = new CombineInstance[meshComponents.Count];
        for( int i = 0; i < meshComponents.Count; ++i )
        {
            combine[i].mesh = meshComponents[i].GetComponent<MeshFilter>().sharedMesh;
            combine[i].transform = meshComponents[i].GetComponent<MeshFilter>().transform.localToWorldMatrix;
        }
        transform.GetComponent<MeshFilter>().mesh = new Mesh();
        transform.GetComponent<MeshFilter>().sharedMesh.CombineMeshes( combine, false, true );
        transform.GetComponent<MeshRenderer>().sharedMaterials = new Material[meshMats.Count];
        for( int i = 0; i < meshMats.Count; ++i )
        {
            transform.GetComponent<MeshRenderer>().sharedMaterials[i] = meshMats[i];
        }
        transform.gameObject.SetActive( true );
    }