Any detailed CombineMesh documentation ?

Hi all,

The CombineMeshes documentation examples just don’t work and I’m currently making a Tetris/Columns type game so combining a “block’s” meshes is something i need to maintain performance.

As such I need to combine and break-apart meshes on a regular basis … is there anywhere I can get working and meaningful documentation. Currently I’m getting results that render child-meshes in the wrong position or fail to break them apart correctly…

Has anyone covered this functionality is detail?

Thanks,

Clueless

Hi all

I managed to answer my own question by trawling through the docs and searching the forums.

As I hate to find a question via google/forum-search, I my self am struggling with, only to find it also has no answers so i’ll post my findings:

1: Always position parents to Vector3.zero before assigning children.

2: Only Group objects that share the same material.

3: The code bellow allows you to combine and break apart meshes… I’m a total Unity newb so if my codes poor or my comments are wrong please correct me :slight_smile:

	void Combine(Transform _parent)
	{				
		MeshFilter[] meshFilters = _parent.GetComponentsInChildren<MeshFilter>();  
		//Get Parent objects Mesh filters
		
		CombineInstance[] combine = new CombineInstance[meshFilters.Length-1];     
		//We want to combine every thing but the parent 

		int cursor = 0; 														   
		//combine will be one element short so user this to reference it in the loop bellow.
		
		for (int i = 1; i < meshFilters.Length; i++)
    	{    		
        	if(meshFilters[i].sharedMesh == null) continue;						  
        	//No mesh found, then carry on to next meshfilter. 
        	            	
        	combine[cursor].mesh = meshFilters[i].sharedMesh;					  
        	//Mesh found; add it to the CombineInstance
        	
        	combine[cursor++].transform = meshFilters[i].transform.localToWorldMatrix; 
        	//Assign transformation         	
        	
        	meshFilters[i].renderer.enabled = false;        					 
        	//Stop original child from drawing.	
    	}
    	_parent.GetComponent<MeshFilter>().mesh = new Mesh();					 
    	//Generate a new mesh for the parent
    	
    	_parent.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);			 
    	//Combine the childrens meshes into the new mesh
    	
    	_parent.renderer.material = meshFilters[1].renderer.sharedMaterial;    	 
    	//Set the material of the parent to that of the children 
    	//(only group children who share the same material with this method!)
    	
	}
	
	void BreakApart(Transform _parent)
	{
		Transform[] _transforms = _parent.GetComponentsInChildren<Transform>();		
		//Get Children
		
		for (int i = 1; i < _transforms.Length; i++)
		  	_transforms[i].renderer.enabled = true;  								
		  	//Start children drawing them selves again.
		  	
		_parent.DetachChildren();	
		//Unparent the children
		
    	_parent.GetComponent<MeshFilter>().mesh = new Mesh();
    	//regenerate parents mesh now it has no children
	}