struct in UnityEngine
/
Implemented in:UnityEngine.CoreModule
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.
CloseFor 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.
CloseA struct containing Mesh data for C# Job System access.
Use a MeshData
struct to access, process and create Meshes in the C# Job System. There are two types of MeshData
struct: read-only MeshData
structs that allow read-only access to Mesh data from the C# Job System, and writeable MeshData
structs that allow you to create Meshes from the C# Job System.
Read-only MeshData
When you pass one or more Meshes to Mesh.AcquireReadOnlyMeshData, Unity returns a MeshDataArray of read-only MeshData
structs. You can access the resulting MeshDataArray
and MeshData
structs from any thread. Creating a MeshDataArray
has some overhead for memory tracking and safety reasons, so it is more efficient to make a single call to Mesh.AcquireReadOnlyMeshData and request multiple MeshData
structs in the same MeshDataArray
than it is to make multiple calls to Mesh.AcquireReadOnlyMeshData.
Each MeshData
struct contains a read-only snapshot of data for a given Mesh. You can use GetIndexData and GetVertexData to access the raw read-only Mesh data without any memory allocations, data copies or format conversions. You need to know the exact Mesh data layout to do this: for instance, the presence and formats of all the mesh vertex attributes.
You can use GetColors, GetIndices, GetNormals, GetTangents, GetUVs, and GetVertices to copy the read-only Mesh data into pre-existing arrays. These methods also perform data format conversions if needed. For example, if the read-only MeshData
struct uses VertexAttributeFormat.Float16 normals and you call GetNormals, the normals will be converted into Vector3 normals in the destination array.
You must dispose of the MeshDataArray
once you have finished working with it. Calling Mesh.AcquireReadOnlyMeshData does not cause any memory allocations or data copies by default, as long as you dispose of the MeshDataArray
before modifying the Mesh. However, if you call Mesh.AcquireReadOnlyMeshData and then modify the Mesh while the MeshDataArray
exists, Unity must copy the MeshDataArray
into a new memory allocation. In addition to this, if you call Mesh.AcquireReadOnlyMeshData and then modify the Mesh, your modifications are not reflected in the MeshData
structs.
Use Dispose to dispose of the MeshDataArray
, or use the C# using
pattern to do this automatically:
using Unity.Collections; using UnityEngine; public class ExampleScript : MonoBehaviour { void Start() { var mesh = new Mesh(); mesh.vertices = new[] {Vector3.one, Vector3.zero}; using (var dataArray = Mesh.AcquireReadOnlyMeshData(mesh)) { var data = dataArray[0]; // prints "2" Debug.Log(data.vertexCount); var gotVertices = new NativeArray<Vector3>(mesh.vertexCount, Allocator.TempJob); data.GetVertices(gotVertices); // prints "(1.0, 1.0, 1.0)" and "(0.0, 0.0, 0.0)" foreach (var v in gotVertices) Debug.Log(v); gotVertices.Dispose(); } } }
Writeable MeshData
Use Mesh.AllocateWritableMeshData to obtain a MeshDataArray of writeable MeshData
structs. You can access the resulting MeshDataArray
and MeshData
structs from any thread. Creating a MeshDataArray
has some overhead for memory tracking and safety reasons, so it is more efficient to make a single call to Mesh.AllocateWritableMeshData and request multiple MeshData
structs in the same MeshDataArray
than it is to make multiple calls to Mesh.AllocateWritableMeshData.
You can populate writeable MeshData
structs with data to create new Meshes. Use Mesh.MeshData.SetVertexBufferParams to set the vertex buffer size and layout, and then write to the array returned by Mesh.MeshData.GetVertexData to set the vertices. Use Mesh.MeshData.SetIndexBufferParams to set the index buffer size and format, and then write to the array returned by Mesh.MeshData.GetIndexData to set the indices. Write to Mesh.MeshData.subMeshCount to set the number of sub meshes, and then use Mesh.MeshData.SetSubMesh to set sub mesh data.
When you have populated the writeable MeshData
struct with your data, use Mesh.ApplyAndDisposeWritableMeshData to apply the data to Mesh objects and automatically dispose of the MeshDataArray
.
using UnityEngine; using UnityEngine.Rendering; using Unity.Collections;
[RequireComponent(typeof(MeshFilter))] public class ExampleScript : MonoBehaviour { void Start() { // Allocate mesh data for one mesh. var dataArray = Mesh.AllocateWritableMeshData(1); var data = dataArray[0];
// Tetrahedron vertices with positions and normals. // 4 faces with 3 unique vertices in each -- the faces // don't share the vertices since normals have to be // different for each face. data.SetVertexBufferParams(12, new VertexAttributeDescriptor(VertexAttribute.Position), new VertexAttributeDescriptor(VertexAttribute.Normal, stream: 1));
// Four tetrahedron vertex positions: var sqrt075 = Mathf.Sqrt(0.75f); var p0 = new Vector3(0, 0, 0); var p1 = new Vector3(1, 0, 0); var p2 = new Vector3(0.5f, 0, sqrt075); var p3 = new Vector3(0.5f, sqrt075, sqrt075 / 3);
// The first vertex buffer data stream is just positions; // fill them in. NativeArray<Vector3> pos = data.GetVertexData<Vector3>(); pos[0] = p0; pos[1] = p1; pos[2] = p2; pos[3] = p0; pos[4] = p2; pos[5] = p3; pos[6] = p2; pos[7] = p1; pos[8] = p3; pos[9] = p0; pos[10] = p3; pos[11] = p1;
// Note: normals will be calculated later in RecalculateNormals. // Tetrahedron index buffer: 4 triangles, 3 indices per triangle. // All vertices are unique so the index buffer is just a // 0,1,2,...,11 sequence. data.SetIndexBufferParams(12, IndexFormat.UInt16); NativeArray<ushort> indexBuffer = data.GetIndexData<ushort>(); for (ushort i = 0; i < indexBuffer.Length; ++i) indexBuffer[i] = i;
// One sub-mesh with all the indices. data.subMeshCount = 1; data.SetSubMesh(0, new SubMeshDescriptor(0, indexBuffer.Length)); // Create the mesh and apply data to it: var mesh = new Mesh(); mesh.name = "Tetrahedron"; Mesh.ApplyAndDisposeWritableMeshData(dataArray, mesh); mesh.RecalculateNormals(); mesh.RecalculateBounds(); GetComponent<MeshFilter>().mesh = mesh; } }
indexFormat | Gets the format of the index buffer data in the MeshData. (Read Only) |
subMeshCount | The number of sub-meshes in the MeshData. |
vertexBufferCount | Gets the number of vertex buffers in the MeshData. (Read Only) |
vertexCount | Gets the number of vertices in the MeshData. (Read Only) |
GetColors | Populates an array with the vertex colors from the MeshData. |
GetIndexData | Gets raw data from the index buffer of the MeshData. |
GetIndices | Populates an array with the indices for a given sub-mesh from the MeshData. |
GetNormals | Populates an array with the vertex normals from the MeshData. |
GetSubMesh | Gets data about a given sub-mesh in the MeshData. |
GetTangents | Populates an array with the vertex tangents from the MeshData. |
GetUVs | Populates an array with the UVs from the MeshData. |
GetVertexAttributeDimension | Gets the dimension of a given vertex attribute in the MeshData. |
GetVertexAttributeFormat | Gets the format of a given vertex attribute in the MeshData. |
GetVertexAttributeOffset | Gets the offset within a vertex buffer stream of a given vertex data attribute on this MeshData. |
GetVertexAttributeStream | Get the vertex buffer stream index of a specific vertex data attribute on this MeshData. |
GetVertexBufferStride | Get the vertex buffer stream stride in bytes. |
GetVertexData | Gets raw data for a given vertex buffer stream format in the MeshData. |
GetVertices | Populates an array with the vertex positions from the MeshData. |
HasVertexAttribute | Checks if a given vertex attribute exists in the MeshData. |
SetIndexBufferParams | Sets the index buffer size and format of the Mesh that Unity creates from the MeshData. |
SetSubMesh | Sets the data for a sub-mesh of the Mesh that Unity creates from the MeshData. |
SetVertexBufferParams | Sets the vertex buffer size and layout of the Mesh that Unity creates from the MeshData. |
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.