# SetVertexBufferData

> Sets the data of the vertex buffer of the Mesh.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## SetVertexBufferData\<T>(NativeArray\<T>, int, int, int, int, MeshUpdateFlags)

Sets the data of the vertex buffer of the Mesh.

```csharp
public void SetVertexBufferData<T>(NativeArray<T> data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
```

### Parameters

**** (\[NativeArray\<T>]\(/engine/6000.0/script-reference/unity/collections/nativearray1)): Vertex data array.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in the data to copy from.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in mesh vertex buffer to receive the data.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Number of vertices to copy.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.**** (\[MeshUpdateFlags]\(/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags)): Flags controlling the function behavior, see [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md).

### Remarks

Simple usage of [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) scripting API involves using functions like [Mesh.vertices](/engine/6000.0/script-reference/unityengine/mesh/vertices.md), [Mesh.normals](/engine/6000.0/script-reference/unityengine/mesh/normals.md) to setup vertex data.

For advanced use cases that require maximum performance, you can use the advanced API, which has functions like [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), and [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md). This advanced API gives access to the underlying mesh data structures that primarily work on raw index buffers, vertex buffers and mesh subset data.

You can use SetVertexBufferData to set vertex data directly, without using format conversions for each vertex attribute. The supplied data layout has to match the vertex data layout of the mesh (see [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.GetVertexAttributes](/engine/6000.0/script-reference/unityengine/mesh/getvertexattributes.md)). Partial updates of the data are also possible, via dataStart, meshBufferStart, count parameters.

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Collections;

public class Example : MonoBehaviour
{
    // Vertex with FP32 position, FP16 2D normal and a 4-byte tangent.
    // In some cases StructLayout attribute needs
    // to be used, to get the data layout match exactly what it needs to be.
    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    struct ExampleVertex
    {
        public Vector3 pos;
        public ushort normalX, normalY;
        public Color32 tangent;
    }

    void Start()
    {
        var mesh = new Mesh();
        // specify vertex count and layout
        var layout = new[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float16, 2),
            new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.UNorm8, 4),
        };
        var vertexCount = 10;
        mesh.SetVertexBufferParams(vertexCount, layout);

        // set vertex data
        var verts = new NativeArray<ExampleVertex>(vertexCount, Allocator.Temp);

        // ... fill in vertex array data here...

        mesh.SetVertexBufferData(verts, 0, 0, vertexCount);
    }
}
```

Additional Resources: [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md), [Mesh.AcquireReadOnlyMeshData](/engine/6000.0/script-reference/unityengine/mesh/acquirereadonlymeshdata.md).

## SetVertexBufferData\<T>(\`\<>\[], int, int, int, int, MeshUpdateFlags)

Sets the data of the vertex buffer of the Mesh.

```csharp
public void SetVertexBufferData<T>(T[] data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
```

### Parameters

**** (\[\<T>\[]]\(./)): Vertex data array.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in the data to copy from.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in mesh vertex buffer to receive the data.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Number of vertices to copy.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.**** (\[MeshUpdateFlags]\(/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags)): Flags controlling the function behavior, see [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md).

### Remarks

Simple usage of [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) scripting API involves using functions like [Mesh.vertices](/engine/6000.0/script-reference/unityengine/mesh/vertices.md), [Mesh.normals](/engine/6000.0/script-reference/unityengine/mesh/normals.md) to setup vertex data.

For advanced use cases that require maximum performance, you can use the advanced API, which has functions like [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), and [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md). This advanced API gives access to the underlying mesh data structures that primarily work on raw index buffers, vertex buffers and mesh subset data.

You can use SetVertexBufferData to set vertex data directly, without using format conversions for each vertex attribute. The supplied data layout has to match the vertex data layout of the mesh (see [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.GetVertexAttributes](/engine/6000.0/script-reference/unityengine/mesh/getvertexattributes.md)). Partial updates of the data are also possible, via dataStart, meshBufferStart, count parameters.

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Collections;

public class Example : MonoBehaviour
{
    // Vertex with FP32 position, FP16 2D normal and a 4-byte tangent.
    // In some cases StructLayout attribute needs
    // to be used, to get the data layout match exactly what it needs to be.
    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    struct ExampleVertex
    {
        public Vector3 pos;
        public ushort normalX, normalY;
        public Color32 tangent;
    }

    void Start()
    {
        var mesh = new Mesh();
        // specify vertex count and layout
        var layout = new[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float16, 2),
            new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.UNorm8, 4),
        };
        var vertexCount = 10;
        mesh.SetVertexBufferParams(vertexCount, layout);

        // set vertex data
        var verts = new NativeArray<ExampleVertex>(vertexCount, Allocator.Temp);

        // ... fill in vertex array data here...

        mesh.SetVertexBufferData(verts, 0, 0, vertexCount);
    }
}
```

Additional Resources: [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md), [Mesh.AcquireReadOnlyMeshData](/engine/6000.0/script-reference/unityengine/mesh/acquirereadonlymeshdata.md).

## SetVertexBufferData\<T>(List\<T>, int, int, int, int, MeshUpdateFlags)

Sets the data of the vertex buffer of the Mesh.

```csharp
public void SetVertexBufferData<T>(List<T> data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
```

### Parameters

**** (\[List\<T>]\(https\://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)): Vertex data array.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in the data to copy from.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The first element in mesh vertex buffer to receive the data.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Number of vertices to copy.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.**** (\[MeshUpdateFlags]\(/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags)): Flags controlling the function behavior, see [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md).

### Remarks

Simple usage of [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) scripting API involves using functions like [Mesh.vertices](/engine/6000.0/script-reference/unityengine/mesh/vertices.md), [Mesh.normals](/engine/6000.0/script-reference/unityengine/mesh/normals.md) to setup vertex data.

For advanced use cases that require maximum performance, you can use the advanced API, which has functions like [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), and [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md). This advanced API gives access to the underlying mesh data structures that primarily work on raw index buffers, vertex buffers and mesh subset data.

You can use SetVertexBufferData to set vertex data directly, without using format conversions for each vertex attribute. The supplied data layout has to match the vertex data layout of the mesh (see [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.GetVertexAttributes](/engine/6000.0/script-reference/unityengine/mesh/getvertexattributes.md)). Partial updates of the data are also possible, via dataStart, meshBufferStart, count parameters.

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Collections;

public class Example : MonoBehaviour
{
    // Vertex with FP32 position, FP16 2D normal and a 4-byte tangent.
    // In some cases StructLayout attribute needs
    // to be used, to get the data layout match exactly what it needs to be.
    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    struct ExampleVertex
    {
        public Vector3 pos;
        public ushort normalX, normalY;
        public Color32 tangent;
    }

    void Start()
    {
        var mesh = new Mesh();
        // specify vertex count and layout
        var layout = new[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float16, 2),
            new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.UNorm8, 4),
        };
        var vertexCount = 10;
        mesh.SetVertexBufferParams(vertexCount, layout);

        // set vertex data
        var verts = new NativeArray<ExampleVertex>(vertexCount, Allocator.Temp);

        // ... fill in vertex array data here...

        mesh.SetVertexBufferData(verts, 0, 0, vertexCount);
    }
}
```

Additional Resources: [Mesh.SetVertexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setvertexbufferparams.md), [Mesh.SetIndexBufferParams](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.0/script-reference/unityengine/mesh/setindexbufferdata.md), [Mesh.SetSubMesh](/engine/6000.0/script-reference/unityengine/mesh/setsubmesh.md), [MeshUpdateFlags](/engine/6000.0/script-reference/unityengine/rendering/meshupdateflags.md), [Mesh.AcquireReadOnlyMeshData](/engine/6000.0/script-reference/unityengine/mesh/acquirereadonlymeshdata.md).
