暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayExtensions.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Threading;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using UnityEngine.Assertions;
  6. using UnityEngine.Jobs;
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// Array utilities functions
  11. /// </summary>
  12. public static class ArrayExtensions
  13. {
  14. /// <summary>
  15. /// Resizes a native array. If an empty native array is passed, it will create a new one.
  16. /// </summary>
  17. /// <typeparam name="T">The type of the array</typeparam>
  18. /// <param name="array">Target array to resize</param>
  19. /// <param name="capacity">New size of native array to resize</param>
  20. public static void ResizeArray<T>(this ref NativeArray<T> array, int capacity) where T : struct
  21. {
  22. var newArray = new NativeArray<T>(capacity, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
  23. if (array.IsCreated)
  24. {
  25. NativeArray<T>.Copy(array, newArray, array.Length);
  26. array.Dispose();
  27. }
  28. array = newArray;
  29. }
  30. /// <summary>
  31. /// Resizes a transform access array.
  32. /// </summary>
  33. /// <param name="array">Target array to resize</param>
  34. /// <param name="capacity">New size of transform access array to resize</param>
  35. public static void ResizeArray(this ref TransformAccessArray array, int capacity)
  36. {
  37. var newArray = new TransformAccessArray(capacity);
  38. if (array.isCreated)
  39. {
  40. for (int i = 0; i < array.length; ++i)
  41. newArray.Add(array[i]);
  42. array.Dispose();
  43. }
  44. array = newArray;
  45. }
  46. /// <summary>
  47. /// Resizes an array. If a null reference is passed, it will allocate the desired array.
  48. /// </summary>
  49. /// <typeparam name="T">The type of the array</typeparam>
  50. /// <param name="array">Target array to resize</param>
  51. /// <param name="capacity">New size of array to resize</param>
  52. public static void ResizeArray<T>(ref T[] array, int capacity)
  53. {
  54. if (array == null)
  55. {
  56. array = new T[capacity];
  57. return;
  58. }
  59. Array.Resize<T>(ref array, capacity);
  60. }
  61. /// <summary>
  62. /// Fills an array with the same value.
  63. /// </summary>
  64. /// <typeparam name="T">The type of the array</typeparam>
  65. /// <param name="array">Target array to fill</param>
  66. /// <param name="value">Value to fill</param>
  67. /// <param name="startIndex">Start index to fill</param>
  68. /// <param name="length">The number of entries to write, or -1 to fill until the end of the array</param>
  69. public static void FillArray<T>(this ref NativeArray<T> array, in T value, int startIndex = 0, int length = -1) where T : unmanaged
  70. {
  71. Assert.IsTrue(startIndex >= 0);
  72. unsafe
  73. {
  74. T* ptr = (T*)array.GetUnsafePtr<T>();
  75. int endIndex = length == -1 ? array.Length : startIndex + length;
  76. for (int i = startIndex; i < endIndex; ++i)
  77. ptr[i] = value;
  78. }
  79. }
  80. }
  81. }