Skip to content

Instantly share code, notes, and snippets.

@jkredz
Created February 4, 2019 00:45
Show Gist options
  • Save jkredz/1db24d7b2ae4abab2abbeb09a791e301 to your computer and use it in GitHub Desktop.
Save jkredz/1db24d7b2ae4abab2abbeb09a791e301 to your computer and use it in GitHub Desktop.
Copy from native array to managed ECS utility snippet
public static unsafe void CopyToFast<T>(this NativeArray<T> nativeArray, T[] array) where T : struct {
int byteLength = nativeArray.Length * UnsafeUtility.SizeOf(typeof(T));
void* managedBuffer = UnsafeUtility.AddressOf(ref array[0]);
void* nativeBuffer = nativeArray.GetUnsafePtr();
UnsafeUtility.MemCpy(managedBuffer, nativeBuffer, byteLength);
}
// Usage
NativeArray<Vector3> nativeVertices = new NativeArray<Vector3>(vertexCount, Allocator.Persistent);
Vector3[] managedVertices = new Vector3[vertexCount];
nativeVertices.CopyToFast(managedVertices);
//From https://coffeebraingames.wordpress.com/2018/10/14/some-ecs-utility-scripts/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment