Unity3d static object Optimization series three

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.