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