Back to Entitycomponentsystemsamples

Unity.Collections cheat sheet

EntitiesSamples/Docs/cheatsheet/collections.md

latest10.9 KB
Original Source

Unity.Collections cheat sheet

The collections provided by this package fall into three categories:

  • The collection types in Unity.Collections whose names start with Native- have safety checks for ensuring that they're properly disposed and are used in a thread-safe manner.
  • The collection types in Unity.Collections.LowLevel.Unsafe whose names start with Unsafe- do not have these safety checks.
  • The remaining collection types are not allocated and contain no pointers, so effectively their disposal and thread safety are never a concern. These types hold only small amounts of data.

Allocators

  • Allocator.Temp: The fastest allocator. For very short-lived allocations. Temp allocations cannot be passed into jobs.
  • Allocator.TempJob: The next fastest allocator. For short-lived allocations (4-frame lifetime). TempJob allocations can be passed into jobs.
  • Allocator.Persistent: The slowest allocator. For indefinite lifetime allocations. Persistent allocations can be passed into jobs.

Array-like types

A few key array-like types are provided by the core module, including Unity.Collections.NativeArray<T> and Unity.Collections.NativeSlice<T>. This package itself provides:

NativeListA resizable list.
UnsafeListA resizable list.
UnsafePtrListA resizable list of pointers.
NativeStreamA set of append-only, untyped buffers.
UnsafeStreamA set of append-only, untyped buffers.
UnsafeAppendBufferAn append-only untyped buffer.
NativeQueueA resizable queue.
UnsafeRingQueueA fixed-size circular buffer.
FixedList32BytesA 32-byte list, including 2 bytes of overhead, so 30 bytes are available for storage. Max capacity depends upon the type parameter.
FixedList64BytesA 64-byte list, including 2 bytes of overhead, so 62 bytes are available for storage. Max capacity depends upon the type parameter.
FixedList128BytesA 128-byte list, including 2 bytes of overhead, so 126 bytes are available for storage. Max capacity depends upon the type parameter.
FixedList512BytesA 512-byte list, including 2 bytes of overhead, so 510 bytes are available for storage. Max capacity depends upon the type parameter.
FixedList4096BytesA 4096-byte list, including 2 bytes of overhead, so 4094 bytes are available for storage. Max capacity depends upon the type parameter.

There are no multi-dimensional array types, but you can simply pack multi-dimensional data into a single-dimension: for example, for an int[4][5] array, use an int[20] array instead (because 4 * 5 is 20).

When using the Entities package, a DynamicBuffer component is often the best choice for an array- or list-like collection.

See also NativeArrayExtensions, ListExtensions, NativeSortExtension.

Map and set types

NativeParallelHashMapAn unordered associative array of key-value pairs.
UnsafeParallelHashMapAn unordered associative array of key-value pairs.
NativeParallelHashSetA set of unique values.
UnsafeParallelHashSetA set of unique values.
NativeMultiHashMapAn unordered associative array of key-value pairs. The keys do not have to be unique, i.e. two pairs can have equal keys.
UnsafeMultiHashMapAn unordered associative array of key-value pairs. The keys do not have to be unique, i.e. two pairs can have equal keys.

See also HashSetExtensions, Unity.Collections.NotBurstCompatible.Extensions, and Unity.Collections.LowLevel.Unsafe.NotBurstCompatible.Extensions

Bit arrays and bit fields

BitField32A fixed-size array of 32 bits.
BitField64A fixed-size array of 64 bits.
NativeBitArrayAn arbitrary-sized array of bits.
UnsafeBitArrayAn arbitrary-sized array of bits.

String types

NativeTextA UTF-8 encoded string. Mutable and resizable.
FixedString32BytesA 32-byte UTF-8 encoded string, including 3 bytes of overhead, so 29 bytes available for storage.
FixedString64BytesA 64-byte UTF-8 encoded string, including 3 bytes of overhead, so 61 bytes available for storage.
FixedString128BytesA 128-byte UTF-8 encoded string, including 3 bytes of overhead, so 125 bytes available for storage.
FixedString512BytesA 512-byte UTF-8 encoded string, including 3 bytes of overhead, so 509 bytes available for storage.
FixedString4096BytesA 4096-byte UTF-8 encoded string, including 3 bytes of overhead, so 4093 bytes available for storage.

See also FixedString and FixedStringMethods.

Other types

NativeReferenceA reference to a single value. Functionally equivalent to an array of length 1.
UnsafeAtomicCounter32A 32-bit atomic counter.
UnsafeAtomicCounter64A 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.

Parallel readers and writers

Several of the collection types have nested types for reading and writing from parallel jobs. For example, to write safely to a NativeList<T> from a parallel job, you need a NativeList<T>.ParallelWriter.