Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

Mesh.SetIndexBufferData

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public void SetIndexBufferData(NativeArray<T> data, int dataStart, int meshBufferStart, int count, MeshUpdateFlags flags);

Declaration

public void SetIndexBufferData(T[] data, int dataStart, int meshBufferStart, int count, MeshUpdateFlags flags);

Declaration

public void SetIndexBufferData(List<T> data, int dataStart, int meshBufferStart, int count, MeshUpdateFlags flags);

Parameters

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.

Description

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);
}