we went on to the series two questions continue to explain, the problem in series two is that if more than one material is the same, it is not optimized, then we will optimize it, the first to find out where to optimize, we look at the following for Loop statement is also the series two code, as follows:
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; &nBSP; (combineinstancearrays[materialarrayindex] as arraylist). ADD (combineinstance); }
This code does not determine whether there is the same material, and then we re-modify it:
for (int s = 0; s < meshfilter.sharedmesh.submeshcount; s++) { int Materialarrayindex = contains (Materials, meshrenderer.sharedmaterials[s].name); if (materialarrayindex == -1) { materials. ADD (Meshrenderer.sharedmaterials[s]); materialarrayindex = materials. count - 1; } 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); }
we see a function on it. Contains it is used to determine if there is the same material, if there is a material, the contains function code is as follows:
private int Contains (ArrayList searchlist, String searchname) {for (int i = 0; i < Searchlist.count; i++) {if (((Material) searchlist[i]). Name = = Searchname) {return i; }} return-1; }
The effect is to return an index of the same material, with the following effects:
650) this.width=650; "title=" 1.png "src=" Http://s3.51cto.com/wyfs02/M02/58/D1/wKiom1S9uuXjP3PvAAYPkBx1Gig256.jpg " alt= "Wkiom1s9uuxjp3pvaaypkbx1gig256.jpg"/>
Look at the red part, there are two materials are the same, the material only shows one, it is optimized. The optimization of the whole static object has been solved for everyone.
This article is from the "Kaiyukan mobile" blog, so be sure to keep this source http://jxwgame.blog.51cto.com/943299/1606013
Unity3d static object Optimization series three