Created
February 4, 2019 00:45
-
-
Save jkredz/1db24d7b2ae4abab2abbeb09a791e301 to your computer and use it in GitHub Desktop.
Copy from native array to managed ECS utility snippet
This file contains hidden or 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
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