暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PinnedArray.cs 1.2KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace UnityEngine.Rendering.Universal
  6. {
  7. unsafe struct PinnedArray<T> : IDisposable where T : struct
  8. {
  9. public T[] managedArray;
  10. public GCHandle handle;
  11. public NativeArray<T> nativeArray;
  12. public int length => managedArray != null ? managedArray.Length : 0;
  13. public PinnedArray(int length)
  14. {
  15. managedArray = new T[length];
  16. handle = GCHandle.Alloc(managedArray, GCHandleType.Pinned);
  17. nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>((void*)handle.AddrOfPinnedObject(), length, Allocator.None);
  18. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  19. NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref nativeArray, AtomicSafetyHandle.Create());
  20. #endif
  21. }
  22. public void Dispose()
  23. {
  24. if (managedArray == null) return;
  25. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  26. AtomicSafetyHandle.Release(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(nativeArray));
  27. #endif
  28. handle.Free();
  29. this = default;
  30. }
  31. }
  32. }