using System; using System.Threading; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.Assertions; using UnityEngine.Jobs; namespace UnityEngine.Rendering { /// /// Array utilities functions /// public static class ArrayExtensions { /// /// Resizes a native array. If an empty native array is passed, it will create a new one. /// /// The type of the array /// Target array to resize /// New size of native array to resize public static void ResizeArray(this ref NativeArray array, int capacity) where T : struct { var newArray = new NativeArray(capacity, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); if (array.IsCreated) { NativeArray.Copy(array, newArray, array.Length); array.Dispose(); } array = newArray; } /// /// Resizes a transform access array. /// /// Target array to resize /// New size of transform access array to resize public static void ResizeArray(this ref TransformAccessArray array, int capacity) { var newArray = new TransformAccessArray(capacity); if (array.isCreated) { for (int i = 0; i < array.length; ++i) newArray.Add(array[i]); array.Dispose(); } array = newArray; } /// /// Resizes an array. If a null reference is passed, it will allocate the desired array. /// /// The type of the array /// Target array to resize /// New size of array to resize public static void ResizeArray(ref T[] array, int capacity) { if (array == null) { array = new T[capacity]; return; } Array.Resize(ref array, capacity); } /// /// Fills an array with the same value. /// /// The type of the array /// Target array to fill /// Value to fill /// Start index to fill /// The number of entries to write, or -1 to fill until the end of the array public static void FillArray(this ref NativeArray array, in T value, int startIndex = 0, int length = -1) where T : unmanaged { Assert.IsTrue(startIndex >= 0); unsafe { T* ptr = (T*)array.GetUnsafePtr(); int endIndex = length == -1 ? array.Length : startIndex + length; for (int i = startIndex; i < endIndex; ++i) ptr[i] = value; } } } }