In the series one, we already know the problem, one is the optimization of the position of our model, and the other is if the object of different materials are optimized together, the objects of different materials will disappear, we in the series two are mainly to solve the two problems:
The next step is to find all the meshfilter, and we separate the objects we need to optimize based on the different materials. This requires us to define two linked lists:
ArrayList materials = new ArrayList (); ArrayList combineinstancearrays = new ArrayList ();
Now we're going to go through the meshfilter and meshrenderer that we need to refine the object because the material is the code associated with Meshrenderder as follows:
foreach (gameobject obj in objects) { if (!obj) continue; MeshFilter[] Meshfilters = obj. Getcomponentsinchildren<meshfilter> (); foreach (meshfilter meshfilter in meshfilters) { MeshRenderer meshRenderer = meshFilter.GetComponent<MeshRenderer> (); if  (!meshrenderer) { debug.logerror ("meshfilter does not have a coresponding Meshrenderer. "); continue; } if (Meshrenderer.materials.length != meshfilter.sharedmesh.submeshcount) { debug.logerror (" Mismatch between material count and submesh count. is this the correct Meshrenderer? "); continue; } for (int s = 0; s < meshfilter.sharedmesh.submeshcount; s++) { int materialArrayIndex = 0; for (materialarrayindex = 0; materialarrayindex < materials. count; materialarrayindex++) { if (materials[ Materialarrayindex] == meshrenderer.sharedmaterials[s]) break; } if (materialarrayindex == materials. Count) { materials. ADD (Meshrenderer.sharedmaterials[s]); combineinstancearrays.add (new ArrayList ()); } combineinstance combineinstance = new combineinstance ( ); combineInstance.transform = meshRenderer.transform.localToWorldMatrix; combineinstance.submeshindex = s; combineInstance.mesh = meshfilter.sharedmesh; (combineinstancearrays[materialarrayindex] as arraylist). ADD (combineinstance); } } }
The following is the processing code for Meshfilter:
{ meshfilter meshFilterCombine = gameObject.GetComponent<MeshFilter> (); if (!meshfiltercombine) meshFilterCombine = Gameobject.addcomponent<meshfilter> (); mesh[] meshes = new mesh[materials. count]; combineinstance[] Combineinstances = new combineinstance[materials. count]; for (int m = 0; m < materials. count; m++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&Nbsp; combineinstance[] combineinstancearray = (combineinstancearrays[m] as arraylist). ToArray (typeof (Combineinstance)) as CombineInstance[]; meshes[m] = new mesh (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MESHES[M]. Combinemeshes (combineinstancearray, true, true); combineinstances[m] = new combineinstance (); combineinstances[m].mesh = meshes[m]; combineinstances[m].submeshindex = 0; } meshfiltercombine.sharedmesh = new mesh (); meshfiltercombine.sharedmesh.combinemeshes ( Combineinstances, false, false); foreach (mesh mesh in meshes) { mesh. Clear (); Destroyimmediate (Mesh); } }
Combine it into a mesh, and then the final step to create the mesh renderer and assign the material to it
{Meshrenderer meshrenderercombine = gameobject.getcomponent<meshrenderer> (); if (!meshrenderercombine) Meshrenderercombine = gameobject.addcomponent<meshrenderer> (); material[] Materialsarray = materials. ToArray (typeof (Material)) as material[]; Meshrenderercombine.materials = Materialsarray; } }
Let's look at the changes before and after the optimization, first we use four cubes and a sphere, three kinds of materials. As follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/58/C0/wKioL1S7oeWiYeqRAAHFHA8YuNg634.jpg "title=" 1.png " alt= "Wkiol1s7oewiyeqraahfha8yung634.jpg"/>
Let's show you our optimized graphs:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/58/C0/wKioL1S7omrwXel6AAVMcI0OlAI656.jpg "title=" 2.png " alt= "Wkiol1s7omrwxel6aavmci0olai656.jpg"/>
In contrast we can see that saved by batching is reduced, used textures less, Render textures uses less texture size, although the relative draw call increases, but because Render textures is less, Our optimization effect has been achieved, and the position has not changed, a variety of materials are no problem, but there is a problem, is that our cube has three is the same, but we look at its material:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/58/C0/wKioL1S7o2bSHknmAAC14p3vx0w234.jpg "title=" 3.png " alt= "Wkiol1s7o2bshknmaac14p3vx0w234.jpg"/>
The same material has been written three times, which shows that our program needs to continue to optimize, the above code as long as the combination of it can be used, we will continue to optimize our optimization program in the Podium optimization series three .
This article is from the "Kaiyukan mobile" blog, so be sure to keep this source http://jxwgame.blog.51cto.com/943299/1605373
Unity3d static Object Optimization series two