| Parameter | Description |
|---|---|
| data | The source data container containing the index data is copied. |
| dataStart | The first element in the source data to copy from. |
| meshBufferStart | The first element in the mesh index buffer to receive the data. |
| count | The number of elements to copy from the data container into the Mesh buffer. |
| flags | The optional MeshUpdateFlags flags that control the update behavior of the mesh. |
Sets the data of the mesh's index buffer directly copied from data source.
This method does not update sub-mesh ranges or sub-mesh count. You must configure sub-meshes separately via SetSubMesh to define index ranges and topology, otherwise your mesh will not render.
By default, the data array is validated for out-of-bounds indices. To disable validation for additional performance, set the flags parameter to MeshUpdateFlags.DontValidateIndices. Only disable validation if you are certain your data is correct, as invalid indices can cause crashes or undefined behavior.
This is a high-performance, low-level method for populating mesh index data. It provides direct access to the underlying index buffer with minimal validation overhead, making it suitable for performance-critical scenarios where you need maximum control over mesh data.
For simpler ways to set indices that handle sub-mesh configuration automatically, use SetTriangles or SetIndices instead.
// Generates a procedural triangle mesh with 16-bit indices // Attach this script to a GameObject and enter Play mode. using Unity.Collections; using UnityEngine; using UnityEngine.Rendering; [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] class SetIndexBufferParams : MonoBehaviour { Mesh m_Mesh; void Start() { // Create mesh with predefined bounds to avoid recalculation m_Mesh = new Mesh { name = "SampleMesh", bounds = new Bounds(Vector3.zero, Vector3.one * 2f), vertices = new Vector3[] { new(-1f, 0f), new(0f, Mathf.Sqrt(2f)), new(1f, 0f) } }; // Create and assign triangle indices using (var indices = new NativeArray<ushort>(new ushort[] { 0, 1, 2 }, Allocator.Temp)) { // Configure index buffer format and allocate space m_Mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt16); // Assigns the generated indices to the mesh m_Mesh.SetIndexBufferData(indices, 0, 0, indices.Length); // Define single sub-mesh spanning all indices m_Mesh.SetSubMesh(0, new SubMeshDescriptor(0, indices.Length)); } // Assign mesh to MeshFilter & MeshRenderer GetComponent<MeshFilter>().sharedMesh = m_Mesh; GetComponent<MeshRenderer>().material = GraphicsSettings.currentRenderPipeline != null ? GraphicsSettings.currentRenderPipeline.defaultMaterial : new Material(Shader.Find("Standard")); } void OnDestroy() => Destroy(m_Mesh); }
Additional resources: SetIndexBufferParams, SetVertexBufferData, MeshUpdateFlags.