CombineInstance
A struct that describes a single mesh to be merged with other instances into a combined mesh.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Struct
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public struct CombineInstance
Remarks
Use this struct with the Mesh.CombineMeshes method to merge multiple meshes into a single combined mesh. You must create a for each mesh you want to combine. If a mesh contains multiple sub-meshes, you must create a separate for each sub-mesh.
CombineInstanceCombineInstanceCombineInstanceusing UnityEngine;// This script combines the meshes from children into a single new Mesh.// The combined mesh is assigned to the MeshFilter of this GameObject.// The original meshes are not destroyed, but their GameObjects are disabled.[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]public class ExampleClass : MonoBehaviour{ void Start() { MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>(); CombineInstance[] instances = new CombineInstance[meshFilters.Length]; for (int i = 0; i < meshFilters.Length; i++) { var meshFilter = meshFilters[i]; instances[i] = new CombineInstance { mesh = meshFilter.sharedMesh, transform = meshFilter.transform.localToWorldMatrix, }; meshFilter.gameObject.SetActive(false); } Mesh combinedMesh = new Mesh(); combinedMesh.CombineMeshes(instances); gameObject.GetComponent<MeshFilter>().sharedMesh = combinedMesh; gameObject.SetActive(true); }}
Additional Resources: Mesh.CombineMeshes, Mesh.subMeshCount.
Properties
Property | Description |
|---|---|
| lightmapScaleOffset | The baked lightmap UV scale and offset applied to the Mesh. |
| mesh | The Mesh to be combined with the other meshes. |
| subMeshIndex | The index of the sub-mesh to be combined with the other meshes. |
| transform | A matrix used to transform vertices before combining meshes. |