Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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
CombineInstance
struct with the Mesh.CombineMeshes method to merge multiple meshes into a single combined mesh. You must create a
CombineInstance
for each mesh you want to combine. If a mesh contains multiple sub-meshes, you must create a separate
CombineInstance
for each sub-mesh.
using 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

lightmapScaleOffsetThe baked lightmap UV scale and offset applied to the Mesh.
meshThe Mesh to be combined with the other meshes.
subMeshIndexThe index of the sub-mesh to be combined with the other meshes.
transformA matrix used to transform vertices before combining meshes.