Version: Unity 7.0 Alpha (7000.0)
Language : English
Introduction to unmanaged collections
Parallel readers and writers

Collection types reference

This section outlines some key collection types you might want to use in your jobs and Burst compiled code.

Array-like types

The Unity.Collections namespace includes the following array-like types:

Data structure Description
NativeArray<T> A fixed-size block of unmanaged memory which you can directly access from managed code.
NativeList<T> A resizable list.
UnsafeList<T> A resizable list.
UnsafePtrList<T> A resizable list of pointers.
NativeSlice<T> Provides a view on a buffer of native memory most commonly acquired from a NativeArray<T>.
NativeStream A set of append-only, untyped buffers.
UnsafeStream A set of append-only, untyped buffers.
UnsafeAppendBuffer An append-only untyped buffer.
NativeQueue<T> A resizable queue.
NativeRingQueue<T> A fixed-size circular buffer.
UnsafeRingQueue<T> A fixed-size circular buffer.
FixedList32Bytes<T> A 32-byte list, which includes 2 bytes of overhead, so 30 bytes are available for storage. Max capacity depends upon T. FixedList32Bytes<T> has variants of larger sizes:
- FixedList64Bytes<T>
- FixedList128Bytes<T>
- FixedList512Bytes<T>
- FixedList4096Bytes<T>

There aren’t any multi-dimensional array types, but you can pack all the data into a single dimension. For example, for an int[4][5] array, use an int[20] array instead (because 4 * 5 is 20). Then you can access element [i,j] with array[i*4 + j].

Additionally, there are various extension methods in NativeArrayExtensions, ListExtensions, and NativeSortExtension.

Map and set types

The following collection types have low memory overhead and are suitable for use in single-threaded cases:

Data structure Description
NativeHashMap<TKey, TValue> An unordered associative array of key-value pairs.
UnsafeHashMap<TKey, TValue> An unordered associative array of key-value pairs.
NativeHashSet<T> A set of unique values.
UnsafeHashSet<T> A set of unique values.

The following collection types have higher memory overhead and are more appropriate for multithreaded situations:

Data structure Description
NativeParallelHashMap<TKey, TValue> An unordered associative array of key-value pairs.
UnsafeParallelHashMap<TKey, TValue> An unordered associative array of key value pairs.
NativeParallelHashSet<T> A set of unique values.
UnsafeParallelHashSet<T> A set of unique values.
NativeParallelMultiHashMap<TKey, TValue> An unordered associative array of key value pairs. The keys don’t have to be unique. For example, two pairs can have equal keys.
UnsafeParallelMultiHashMap<TKey, TValue> An unordered associative array of key value pairs. The keys don’t have to be unique. For example, two pairs can have equal keys.

Additionally, there are various extension methods in NotBurstCompatible.Extensions and Unsafe.NotBurstCompatible.Extensions.

Bit arrays and bit fields

The following are arrays of bits:

Data structure Description
BitField32 A fixed-size array of 32 bits.
BitField64 A fixed-size array of 64 bits.
NativeBitArray An arbitrary sized array of bits.
UnsafeBitArray An arbitrary-sized array of bits.

String types

The following are string types:

Data structure Description
NativeText A UTF–8 encoded string. Mutable and resizable.
UnsafeText A UTF–8 encoded string. Mutable and resizable.
FixedString32Bytes A 32-byte UTF–8 encoded string, including 3 bytes of overhead, so 29 bytes available for storage.
FixedString64Bytes A 64-byte UTF–8 encoded string, including 3 bytes of overhead, so 61 bytes available for storage.
FixedString128Bytes A 128-byte UTF–8 encoded string, including 3 bytes of overhead, so 125 bytes available for storage.
FixedString512Bytes A 512-byte UTF–8 encoded string, including 3 bytes of overhead, so 509 bytes available for storage.
FixedString4096Bytes A 4096-byte UTF–8 encoded string, including 3 bytes of overhead, so 4093 bytes available for storage.

There are further extension methods in FixedStringMethods.

Other types

Data structure Description
NativeReference<T> A reference to a single value. Functionally equivalent to an array of length 1.
UnsafeAtomicCounter32 A 32-bit atomic counter.
UnsafeAtomicCounter64 A 64-bit atomic counter.

Enumerators

Most of the collections have a GetEnumerator method, which returns an implementation of IEnumerator<T>. The enumerator’s MoveNext method advances its Current property to the next element:

NativeList<int> nums = new NativeList<int>(10, Allocator.Temp);

// Calculate the sum of all elements in the list.
int sum = 0;
var enumerator = nums.GetEnumerator();

// The first MoveNext call advances the enumerator to the first element.
// MoveNext returns false when the enumerator has advanced past the last element.
while (enumerator.MoveNext())
{
    sum += enumerator.Current;
}

// The enumerator is no longer valid to use after the array is disposed.
nums.Dispose();

Additional resources

Introduction to unmanaged collections
Parallel readers and writers