data | Vertex data array. |
dataStart | The first element in the data to copy from. |
meshBufferStart | The first element in mesh vertex buffer to receive the data. |
count | Number of vertices to copy. |
stream | Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3. |
flags | Flags controlling the function behavior, see MeshUpdateFlags. |
Sets the data of the vertex buffer of the Mesh.
Simple usage of Mesh scripting API involves using functions like vertices, normals to setup vertex data.
For advanced use cases that require maximum performance, you can use the advanced API, which has functions like SetSubMesh,
SetIndexBufferParams, SetIndexBufferData, and SetVertexBufferParams. 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 SetVertexBufferParams,
GetVertexAttributes). Partial updates of the data are also possible, via dataStart
, meshBufferStart
, count
parameters.
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: SetVertexBufferParams, SetIndexBufferParams, SetIndexBufferData, SetSubMesh, MeshUpdateFlags, AcquireReadOnlyMeshData.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.