Zero-allocation copying of Meshes using the new MeshData functionality in Unity 2020.1+.
- Testedwith Unity 6000.0.52f1 LTS
- BREAKING CHANGES:
- Refactored the project structure to conform to Unity package naming and folder conventions. The package must be added using a different name and path (See below).
- The
CopyReplicate
method no longer creates a new Mesh. It accepts a reference to a Mesh object, just like theCopyTo
method.
The code comes in the form of the static class FastMeshCopy
with the
public static void CopyTo(this Mesh inMesh, ref Mesh outMesh){...}
and
public static void CopyReplicate(this Mesh inMesh, ref Mesh outMesh, NativeArray<float4x4> matrices) {...}
extension methods.
Simply call this method on a Mesh instance to perform either a Single Copy, or a Multi-Copy based on an array of transformations + Merge.
Add this line to your Packages/manifest.json
file
"com.ulc.fast-mesh-copy": "https://github.com/GeorgeAdamon/FastMeshCopy.git?path=/UnityProject/Packages/com.ulc.fast-mesh-copy#v2.0.0",
- Attach the MeshCopyExample.cs to a GameObject, and reference a Mesh in the inMesh field.
- Run the game, and press the Spacebar to perform the mesh copy.
Accessing BlendShapes and BoneWeights without GC allocations is not supported by Unity's API, so this zero-allocation effort ignores them for the time being. See the discussion
Copying of a 98 MB mesh, creating only 80 Bytes of allocations for the Garbage Collector to clean-up.