using UnityEngine; using Unity.Mathematics; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; public static class ExtensionMethods_NativeArray { public unsafe static NativeArray GetNativeArray ( this SRC[] source ) where SRC : unmanaged where DST : unmanaged { //ASSERTION: sizeof(SRC)==sizeof(DST) // create a destination NativeArray to hold the vertices var nativeArray = new NativeArray( source.Length , Allocator.Persistent , NativeArrayOptions.UninitializedMemory ); // pin the mesh's vertex buffer in place... fixed( void* bufferPointer = source) { // ...and use memcpy to copy the Vector3[] into a NativeArray without casting. whould be fast! UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeArray) , bufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } // we only hve to fix the .net array in place, the NativeArray is allocated in the C++ side of the engine and // wont move arround unexpectedly. We have a pointer to it not a reference! thats basically what fixed does, // we create a scope where its 'safe' to get a pointer and directly manipulate the array return nativeArray; } public unsafe static NativeArray GetNativeArray ( this T[] source ) where T : unmanaged { // create a destination NativeArray to hold the vertices var nativeArray = new NativeArray( source.Length , Allocator.Persistent , NativeArrayOptions.UninitializedMemory ); // pin the mesh's vertex buffer in place... fixed( void* bufferPointer = source) { // ...and use memcpy to copy the Vector3[] into a NativeArray without casting. whould be fast! UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeArray) , bufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } // we only hve to fix the .net array in place, the NativeArray is allocated in the C++ side of the engine and // wont move arround unexpectedly. We have a pointer to it not a reference! thats basically what fixed does, // we create a scope where its 'safe' to get a pointer and directly manipulate the array return nativeArray; } public unsafe static NativeArray GetNativeArray ( this Vector3[] source ) { NativeArray verts = new NativeArray( source.Length , Allocator.Persistent , NativeArrayOptions.UninitializedMemory ); fixed( void* vertexBufferPointer = source) { UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(verts) , vertexBufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } return verts; } public unsafe static NativeArray GetNativeArray ( this int[] source ) { NativeArray verts = new NativeArray(source.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); fixed( void* vertexBufferPointer = source ) { UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(verts) , vertexBufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } return verts; } public unsafe static NativeArray GetNativeArray ( this Vector2[] source ) { NativeArray verts = new NativeArray( source.Length , Allocator.Persistent , NativeArrayOptions.UninitializedMemory ); fixed( void* vertexBufferPointer = source ) { UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(verts) , vertexBufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } return verts; } public unsafe static NativeArray GetNativeArray ( this Vector4[] source ) { NativeArray verts = new NativeArray( source.Length , Allocator.Persistent , NativeArrayOptions.UninitializedMemory ); fixed( void* vertexBufferPointer = source ) { UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(verts) , vertexBufferPointer , source.Length * (long)UnsafeUtility.SizeOf() ); } return verts; } public unsafe static void MemCpy ( this NativeArray source , DST[] target ) where SRC : unmanaged where DST : unmanaged { //ASSERTION: sizeof(SRC)==sizeof(DST) fixed( void* arrayPointer = target ) { UnsafeUtility.MemCpy( arrayPointer , NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(source) , target.Length * (long)UnsafeUtility.SizeOf() ); } } public unsafe static void MemCpy ( this SRC[] src , NativeArray dst ) where SRC : unmanaged where DST : unmanaged { //ASSERTION: sizeof(SRC)==sizeof(DST) //ASSERTION: dst.IsCreated==true fixed( void* bufferPointer = src ) { UnsafeUtility.MemCpy( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(dst) , bufferPointer , src.Length * (long)UnsafeUtility.SizeOf() ); } } }