Unity3d mesh merging and unity3d Mesh

Source: Internet
Author: User

Unity3d mesh merging and unity3d Mesh

Several different objects can be combined in Unity3d to optimize the grid.

Create a temporary scenario in Unity3d as follows: Create a C # script named "CombineMeshes" and mount it on the Cube.

The content of CombineMeshes. cs is as follows:

Using UnityEngine; using System. collections; public class CombineMeshes: MonoBehaviour {void Start () {MeshFilter [] meshFilters = GetComponentsInChildren <MeshFilter> (); // obtain CombineInstance [] combine = new CombineInstance [meshFilters. length]; // create a CombineInstance array for (int I = 0; I <meshFilters. length; I ++) {combine [I]. mesh = meshFilters [I]. sharedMesh; combine [I]. transform = meshFilters [I]. transform. localToWorldMatrix; meshFilters [I]. gameObject. setActive (false);} transform. getComponent <MeshFilter> (). mesh = new Mesh (); transform. getComponent <MeshFilter> (). mesh. combineMeshes (combine); // merge transform. gameObject. setActive (true );}}

 

Save and execute. Result

Observe that the original two sub-objects have changed to the hidden state. Observe the mesh of the Cube, which has changed to the merged mesh of the three objects:

 

Next, let's think about what it will be like if the three objects are assigned different material balls?

Next, let's try our ideas. Modify the scenario by assigning different material balls to the three objects, for example:

If you still use this code, the following results will be returned:

Obviously, although the mesh is merged, There is no code to process the material ball during the merge process. Therefore, the merged mesh is assigned the material ball of the Cube.

So, if we need to merge the mesh and keep the original material balls in three parts, can we achieve this? The answer is yes.

Modify the code in "CombineMeshes. cs" as follows:

Using UnityEngine; using System. collections; public class CombineMeshes: MonoBehaviour {void Start () {MeshFilter [] meshFilters = GetComponentsInChildren <MeshFilter> (); CombineInstance [] combine = new CombineInstance [meshFilters. length]; MeshRenderer [] meshRenderer = GetComponentsInChildren <MeshRenderer> (); // obtain Material [] mats = new Material [MeshRenderer. length]; // create a material ball array for (int I = 0; I <meshFilters. length; I ++) {mats [I] = meshRenderer [I]. sharedMaterial; // obtain the combine [I]. mesh = meshFilters [I]. sharedMesh; combine [I]. transform = meshFilters [I]. transform. localToWorldMatrix; meshFilters [I]. gameObject. setActive (false);} transform. getComponent <MeshFilter> (). mesh = new Mesh (); transform. getComponent <MeshFilter> (). mesh. combineMeshes (combine, false); // mesh. add a false parameter to CombineMeshes, indicating that it is not merged into a grid, but a subgrid list transform. getComponent <MeshRenderer> (). sharedMaterials = mats; // specify the material transform for the merged GameObject. gameObject. setActive (true );}}

After another operation,

.

You can also modify the script to meet more complex requirements. The main reason is that you can flexibly use the grid merging principle. For example, in a loop

meshFilters[i].gameObject.SetActive(false);

Replace

if (meshFilters[i].gameObject.name != gameObject.name){    Destroy(meshFilters[i].gameObject);}

It can be merged to improve the cleanliness. If you are interested, you can try to put these three models under an empty object and see how to modify the code before merging and understanding :)

Later, I will add a method to merge the role skeleton model, that is, to merge the component containing the <SkinnedMeshRenderer>. I remember the first time I came into contact with the plug-in on the official website for a long time.

Link to the Chinese manual with the main statement: http://game.ceeger.com/Script/Mesh/Mesh.CombineMeshes.html


Unity3D project Merging

All parts made by everyone exist in scenarios. During merging, even if the imported resources have the same coverage, the resources can be reused and will not be affected, but the scenarios must be independent.
If you do not want to integrate resources into the server, you can use a package for the integration project. Generally, your resources are stored in your own folder for convenient management, there are overlapping resources when the package is imported, and there is no symbol "new" behind it. you can choose not to import these resources.

If there is coverage, find the same resource first, copy one copy, rename, and import Integration

How to merge models in unity3d

Drag to his directory
 

Related Article

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.