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.