Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetVertexData<T>(int)

Gets raw data for a given vertex buffer stream format in the MeshData.
Read time 1 minuteLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public NativeArray<T> GetVertexData<T>(int stream = 0) where T : struct

Parameters

stream

The vertex buffer stream to get data for. The default value is 0.

Returns

Type

Description

NativeArray<T>Returns a
NativeArray
containing the vertex buffer data.

Remarks

GetVertexData returns a direct "pointer" into the raw vertex buffer data without any memory allocations, data copies or conversions. You do not need to dispose of the returned
NativeArray
, because it does not represent a new memory allocation.
You need to know the exact Mesh data layout to do this: for instance, the presence and formats of all the mesh vertex attributes. The data layout matches the one from Mesh.SetVertexBufferData.
If the
MeshData
is writeable, and you have set the vertex buffer size and layout using Mesh.MeshData.SetVertexBufferParams, you can write vertex data directly to the array. If the
MeshData
is read-only, the array is read-only.
using UnityEngine;public class ExampleScript : MonoBehaviour{ struct Vertex { public Vector3 pos; public Vector2 uv; } void Start() { var mesh = new Mesh(); mesh.vertices = new[] {Vector3.one, Vector3.up}; mesh.uv = new[] {Vector2.right, Vector2.up}; using (var data = Mesh.AcquireReadOnlyMeshData(mesh)) { var verts = data[0].GetVertexData<Vertex>(); // prints "pos (1.0, 1.0, 1.0) uv (1.0, 0.0)" and "pos (0.0, 1.0, 0.0) uv (0.0, 1.0)" foreach (var v in verts) Debug.Log($"pos {v.pos} uv {v.uv}"); } }}