data | 顶点数据数组。 |
dataStart | 要从中进行复制的数据中的第一个元素。 |
meshBufferStart | 用于接收数据的网格顶点缓冲区中的第一个元素。 |
count | 要复制的顶点数。 |
stream | Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3. |
flags | 控制函数行为的标志,请参阅 MeshUpdateFlags。 |
设置网格顶点缓冲区的数据。
Mesh 脚本 API 的简单用法涉及使用 vertices、normals 等函数设置顶点数据。
对于需要最大性能的高级用例,可以使用具有 SetSubMesh、SetIndexBufferParams、SetIndexBufferData 和
SetVertexBufferParams 等函数的高级 API。通过此高级
API 可以访问主要适用于原始索引缓冲区、顶点缓冲区和网格子集数据的基础网格数据结构。
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); } }
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.