From b51afe5c4972e6077348dd3e4a2ef677596d38d2 Mon Sep 17 00:00:00 2001 From: momoma-null Date: Fri, 19 Jun 2026 13:28:14 +0900 Subject: [PATCH] =?UTF-8?q?Memory=E3=82=92NativeArray=E3=81=AB=E3=82=B3?= =?UTF-8?q?=E3=83=94=E3=83=BC=E3=81=99=E3=82=8B=E3=81=A8=E3=81=8D=E3=83=9D?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=BF=E3=83=BC=E3=82=92=E4=BB=8B=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=81=93=E3=81=A8=E3=81=A7=E3=82=B3=E3=83=94=E3=83=BC?= =?UTF-8?q?=E3=82=92=E5=89=8A=E6=B8=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Packages/UniGLTF/Runtime/UniGLTF.asmdef | 2 +- .../Runtime/UniGLTF/IO/NativeArrayManager.cs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) 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; + } } ///