diff --git a/Packages/UniGLTF/Runtime/UniGLTF.asmdef b/Packages/UniGLTF/Runtime/UniGLTF.asmdef index c561cf1b7..91f98fdae 100644 --- a/Packages/UniGLTF/Runtime/UniGLTF.asmdef +++ b/Packages/UniGLTF/Runtime/UniGLTF.asmdef @@ -9,7 +9,7 @@ ], "includePlatforms": [], "excludePlatforms": [], - "allowUnsafeCode": false, + "allowUnsafeCode": true, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, diff --git a/Packages/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs b/Packages/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs index 01f358f95..b8a5deca4 100644 --- a/Packages/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs +++ b/Packages/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs @@ -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 CreateNativeArray(ReadOnlyMemory data) where T : struct { - var array = CreateNativeArray(data.Length); - var toSpan = array.AsSpan(); - var fromSpan = data.Span; - fromSpan.CopyTo(toSpan); - return array; + unsafe + { + var handle = data.Pin(); + var nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray(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; + } } ///