Skip to content

Instantly share code, notes, and snippets.

@samloeschen
Last active October 1, 2020 04:10
Show Gist options
  • Save samloeschen/fbc8a25c5f248cb9f9cea859ec117d71 to your computer and use it in GitHub Desktop.
Save samloeschen/fbc8a25c5f248cb9f9cea859ec117d71 to your computer and use it in GitHub Desktop.
NativeListExtensions
public static class NativeListExtensions {
public static unsafe void UnsafeAddRange<T>(this NativeList<T> nativeList, void* ptr, int length) where T: struct {
int idx = nativeList.Length;
int newCount = idx + length;
if (nativeList.Capacity < newCount) {
nativeList.Resize(nativeList.Capacity + length, NativeArrayOptions.UninitializedMemory);
}
nativeList.Length = newCount;
int stride = UnsafeUtility.SizeOf<T>();
IntPtr bufferStart = (IntPtr)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeList.AsArray());
byte* buffer = (byte*)(bufferStart + (stride * idx));
UnsafeUtility.MemCpy(buffer, ptr, length * (long)stride);
}
public static unsafe void UnsafeAdd<T>(this NativeList<T> nativeList, T element, bool allowExpand = true) where T: struct {
int idx = nativeList.Length;
int newCount = nativeList.Length + 1;
if (newCount > nativeList.Capacity) {
if (allowExpand) {
nativeList.Resize(nativeList.Capacity * 2, NativeArrayOptions.UninitializedMemory);
} else {
return;
}
}
int stride = UnsafeUtility.SizeOf<T>();
void* ptr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeList.AsArray());
UnsafeUtility.WriteArrayElement(ptr, idx, element);
}
public static unsafe void UnsafeAddRange<T>(this NativeList<T> nativeList, NativeList<T> otherList) where T: struct {
nativeList.UnsafeAddRange(otherList.AsArray());
}
public static unsafe void UnsafeAddRange<T>(this NativeList<T> nativeList, NativeArray<T> nativeArray) where T: struct {
void* ptr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeArray);
nativeList.UnsafeAddRange(ptr, nativeArray.Length);
}
public static unsafe void UnsafeAddRange(this NativeList<float> nativeList, float[] array, int length) {
fixed (void* arrayPtr = array) { nativeList.UnsafeAddRange(arrayPtr, length); }
}
public static unsafe void UnsafeAddRange(this NativeList<float4> nativeList, float4[] array, int length) {
fixed (void* arrayPtr = array) { nativeList.UnsafeAddRange(arrayPtr, length); }
}
public static unsafe void UnsafeAddRange(this NativeList<float4> nativeList, Color[] array, int length) {
fixed (void* arrayPtr = array) { nativeList.UnsafeAddRange(arrayPtr, length); }
}
public static unsafe void UnsafeAddRange(this NativeList<float3> nativeList, float3[] array, int length) {
fixed (void* arrayPtr = array) { nativeList.UnsafeAddRange(arrayPtr, length); }
}
public static unsafe void UnsafeAddRange(this NativeList<float3> nativeList, Vector3[] array, int length) {
fixed (void* arrayPtr = array) { nativeList.UnsafeAddRange(arrayPtr, length); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment