Created
November 20, 2021 23:46
-
-
Save bkevelham/f7eb34fbfb0c86ab3bea73a24d449738 to your computer and use it in GitHub Desktop.
Deep copy a Mesh in Unity 2020+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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