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

UnsafeNotBurstCompatible.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. namespace Unity.Collections.LowLevel.Unsafe.NotBurstCompatible
  3. {
  4. /// <summary>
  5. /// Provides some extension methods for various collections.
  6. /// </summary>
  7. public static class Extensions
  8. {
  9. /// <summary>
  10. /// Returns a new managed array with all the elements copied from a set.
  11. /// </summary>
  12. /// <typeparam name="T">The type of elements.</typeparam>
  13. /// <param name="set">The set whose elements are copied to the array.</param>
  14. /// <returns>A new managed array with all the elements copied from a set.</returns>
  15. public static T[] ToArray<T>(this UnsafeHashSet<T> set)
  16. where T : unmanaged, IEquatable<T>
  17. {
  18. var array = set.ToNativeArray(Allocator.TempJob);
  19. var managed = array.ToArray();
  20. array.Dispose();
  21. return managed;
  22. }
  23. /// <summary>
  24. /// Adds the content of a string to this append buffer.
  25. /// </summary>
  26. /// <remarks>The length of the string is written as an int to the buffer before the characters are written.</remarks>
  27. /// <param name="buffer">The buffer to which to add the string.</param>
  28. /// <param name="value">The string to copy.</param>
  29. [NotBurstCompatible]
  30. public static unsafe void AddNBC(ref this UnsafeAppendBuffer buffer, string value)
  31. {
  32. if (value != null)
  33. {
  34. buffer.Add(value.Length);
  35. fixed (char* ptr = value)
  36. {
  37. buffer.Add(ptr, sizeof(char) * value.Length);
  38. }
  39. }
  40. else
  41. {
  42. buffer.Add(-1);
  43. }
  44. }
  45. /// <summary>
  46. /// Returns an unmanaged byte array with a copy of this buffer's contents.
  47. /// </summary>
  48. /// <param name="buffer">This buffer.</param>
  49. /// <returns>An unmanaged byte array with a copy of this buffer's contents.</returns>
  50. [NotBurstCompatible]
  51. public static unsafe byte[] ToBytesNBC(ref this UnsafeAppendBuffer buffer)
  52. {
  53. var dst = new byte[buffer.Length];
  54. fixed (byte* dstPtr = dst)
  55. {
  56. UnsafeUtility.MemCpy(dstPtr, buffer.Ptr, buffer.Length);
  57. }
  58. return dst;
  59. }
  60. #if !NET_DOTS
  61. /// <summary>
  62. /// Reads a string from this buffer reader.
  63. /// </summary>
  64. /// <param name="value">Outputs the string.</param>
  65. /// <param name="reader">This reader.</param>
  66. [NotBurstCompatible]
  67. public static unsafe void ReadNextNBC(ref this UnsafeAppendBuffer.Reader reader, out string value)
  68. {
  69. int length;
  70. reader.ReadNext(out length);
  71. if (length != -1)
  72. {
  73. value = new string('0', length);
  74. fixed (char* buf = value)
  75. {
  76. int bufLen = length * sizeof(char);
  77. UnsafeUtility.MemCpy(buf, reader.ReadNext(bufLen), bufLen);
  78. }
  79. }
  80. else
  81. {
  82. value = null;
  83. }
  84. }
  85. #endif
  86. }
  87. }