Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

NativeCustomSlice.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. namespace UnityEngine.U2D.Animation
  7. {
  8. internal struct NativeCustomSlice<T> where T : struct
  9. {
  10. [NativeDisableUnsafePtrRestriction] public IntPtr data;
  11. public int length;
  12. public int stride;
  13. public static NativeCustomSlice<T> Default()
  14. {
  15. return new NativeCustomSlice<T>
  16. {
  17. data = IntPtr.Zero,
  18. length = 0,
  19. stride = 0
  20. };
  21. }
  22. public unsafe NativeCustomSlice(NativeSlice<T> nativeSlice)
  23. {
  24. data = new IntPtr(nativeSlice.GetUnsafeReadOnlyPtr());
  25. length = nativeSlice.Length;
  26. stride = nativeSlice.Stride;
  27. }
  28. public unsafe NativeCustomSlice(NativeSlice<byte> slice, int length, int stride)
  29. {
  30. this.data = new IntPtr(slice.GetUnsafeReadOnlyPtr());
  31. this.length = length;
  32. this.stride = stride;
  33. }
  34. public unsafe T this[int index]
  35. {
  36. get { return UnsafeUtility.ReadArrayElementWithStride<T>(data.ToPointer(), index, stride); }
  37. }
  38. public int Length
  39. {
  40. get { return length; }
  41. }
  42. }
  43. internal struct NativeCustomSliceEnumerator<T> : IEnumerable<T>, IEnumerator<T> where T : struct
  44. {
  45. private NativeCustomSlice<T> nativeCustomSlice;
  46. private int index;
  47. internal NativeCustomSliceEnumerator(NativeSlice<byte> slice, int length, int stride)
  48. {
  49. nativeCustomSlice = new NativeCustomSlice<T>(slice, length, stride);
  50. index = -1;
  51. Reset();
  52. }
  53. public IEnumerator<T> GetEnumerator()
  54. {
  55. return this;
  56. }
  57. IEnumerator IEnumerable.GetEnumerator()
  58. {
  59. return GetEnumerator();
  60. }
  61. public bool MoveNext()
  62. {
  63. if (++index < nativeCustomSlice.length)
  64. {
  65. return true;
  66. }
  67. return false;
  68. }
  69. public void Reset()
  70. {
  71. index = -1;
  72. }
  73. public T Current => nativeCustomSlice[index];
  74. object IEnumerator.Current => Current;
  75. void IDisposable.Dispose() {}
  76. }
  77. }