Right, so I’ve seen SO MANY SOLUTIONS to this problem, but I guess I’m just being hated today. What’s even worse, I got it working earlier, but then I had to fix some minor gameObject leak, and THEN IT WAS GONE
sob sniff
What I’m doing is combining all ‘similar’ meshes into one mesh. Not sure if it increases my framerate or not, but it’s worth a go. I mean, it cuts my amount of draw calls in half, so that’s great. However, they (I mean my new meshes) have no physical presence. Nothing wants to collide with them. It’s a tragedy, really, because I finally got the darn thing working.
Basically, I’m trying this solution:
http://answers.unity3d.com/questions/7130/how-to-assign-procedural-mesh-to-a-collider
This is my version of it.
// Add collisions
MeshCollider meshc = newLevelObject.AddComponent( typeof(MeshCollider) ) as MeshCollider;
meshc.sharedMesh = newLevelObject.GetComponent<MeshFilter>().mesh; //WHY WON'T YOU WORK YOU PIECE OF SH*T LINE OF CODE
Not much difference, except for the comments.
My whole thing is pretty much this:
private void OptimizeMeshObject ( GameObject currentProp, GameObject [] objectList, basicLevelBehavior prop )
{
int targetRoom = prop.targetRoom;
int i;
basicLevelBehavior currentBehavior;
GameObject currentObject;
// First add current prop to the new gameobject
GameObject newLevelObject = new GameObject( "Level Object:"+targetRoom, typeof( MeshFilter ), typeof( MeshRenderer ), typeof( basicLevelBehavior ) );
newLevelObject.GetComponent<MeshFilter>().mesh = new Mesh();
newLevelObject.renderer.sharedMaterial = currentProp.renderer.sharedMaterial;
newLevelObject.GetComponent<basicLevelBehavior>().targetRoom = targetRoom;
newLevelObject.layer = currentProp.layer; // Don't forget to set the actor collision layers!
AddToMesh( newLevelObject, currentProp );
// Go through all the objects again
for ( i = 0; i < objectList.Length; i += 1 )
{
currentObject = objectList[i];
if (( currentObject != null )( currentObject != currentProp ))
{
currentBehavior = currentObject.GetComponent<basicLevelBehavior>( );
if ( currentBehavior != null )
{
if ( currentBehavior.targetRoom == targetRoom )
{
if ( newLevelObject.renderer.sharedMaterial == currentObject.renderer.sharedMaterial )
AddToMesh( newLevelObject, currentObject );
}
}
}
}
// Remove source
DestroyImmediate( currentProp );
// Add collisions
MeshCollider meshc = newLevelObject.AddComponent( typeof(MeshCollider) ) as MeshCollider;
meshc.sharedMesh = newLevelObject.GetComponent<MeshFilter>().mesh; //WHY WON'T YOU WORK YOU PIECE OF SHIT LINE OF CODE
}
private void AddToMesh ( GameObject targetProp, GameObject sourceProp )
{
Mesh newMesh = new Mesh( );
if ( targetProp.GetComponent<MeshFilter>() == null )
return;
Mesh originalMesh = targetProp.GetComponent<MeshFilter>().mesh;
if ( sourceProp.GetComponent<MeshFilter>() == null )
return;
Mesh sourceMesh = sourceProp.GetComponent<MeshFilter>().mesh;
if ( sourceMesh == null )
return;
CombineInstance [] combine = new CombineInstance[ 2 ];
combine[0].mesh = originalMesh;
combine[0].transform = targetProp.transform.localToWorldMatrix;
combine[1].mesh = sourceMesh;
combine[1].transform = sourceProp.transform.localToWorldMatrix;
newMesh.CombineMeshes( combine, true, true );
targetProp.transform.position = Vector3.zero;
targetProp.transform.rotation = Quaternion.identity;
targetProp.transform.localScale = Vector3.one;
targetProp.GetComponent<MeshFilter>().mesh = newMesh;
targetProp.GetComponent<MeshFilter>().mesh.RecalculateBounds();
//targetProp.GetComponent<MeshFilter>().mesh.Optimize();
// Destroy source
DestroyImmediate( sourceProp );
}
Yeah it’s ugly, but paste it into some external editor and it’ll look a lot more pretty.
Leaving the old colliders there isn’t really an option, because the guns and footsteps and effects and all that look to the render material, not the physical material - dumb move I know - and that system wouldn’t be exactly fun to switch about - arg all the objects I’d have to give different physics materials - and so I would love to get this working.
Summary of Problem:
Procedural mesh created with CombineMeshes seem to not get assigned to the collider.
Any help would be much appreciated.