Skip to content

Instantly share code, notes, and snippets.

@bkevelham
Created November 20, 2021 23:46
Show Gist options
  • Save bkevelham/f7eb34fbfb0c86ab3bea73a24d449738 to your computer and use it in GitHub Desktop.
Save bkevelham/f7eb34fbfb0c86ab3bea73a24d449738 to your computer and use it in GitHub Desktop.
Deep copy a Mesh in Unity 2020+
using UnityEngine;
public class MeshUtil
{
public static Mesh DeepCopy(Mesh sourceMesh)
{
//Instead of getting all a mesh's properties by hand, and re-creating a mesh,
//use the API used for the job system and Burst compiler.
//This seems to work fine when not using either of those.
Mesh meshCopy = new Mesh();
var meshData = Mesh.AcquireReadOnlyMeshData(sourceMesh);
Mesh.ApplyAndDisposeWritableMeshData(meshData, meshCopy); //Takes care of the disposing as well
return meshCopy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment