MemoryをNativeArrayにコピーするときポインターを介することでコピーを削減

This commit is contained in:
momoma-null
2026-06-19 13:28:14 +09:00
committed by hadashiA
parent bddb6f4c65
commit b51afe5c49
2 changed files with 13 additions and 6 deletions

View File

@@ -9,7 +9,7 @@
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,

View File

@@ -3,6 +3,7 @@ using System.Linq;
using System.Collections.Generic;
using Unity.Collections;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
namespace UniGLTF
{
@@ -89,11 +90,17 @@ namespace UniGLTF
public NativeArray<T> CreateNativeArray<T>(ReadOnlyMemory<T> data) where T : struct
{
var array = CreateNativeArray<T>(data.Length);
var toSpan = array.AsSpan();
var fromSpan = data.Span;
fromSpan.CopyTo(toSpan);
return array;
unsafe
{
var handle = data.Pin();
var nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>(handle.Pointer, data.Length, Allocator.Persistent);
m_disposables.Add(nativeArray);
m_disposables.Add(handle);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref nativeArray, AtomicSafetyHandle.Create());
#endif
return nativeArray;
}
}
/// <summary>