目录
Unity.Collections
Collections分为三类:
Unity.Collections
中以Native-
开头的都有一个 safety checks 以确保它们被正确地处理并以线程安全的方式使用。Unity.Collections.LowLevel.Unsafe
下面的,以Unsafe-
开头的不包含safety checks.- 其余的类型没有被 allocated and 不包含 pointers, so effectively their disposal and thread safety are never a concern. 这些类型只保存少量的数据。
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
类似于数组的类型有: Unity.Collections.NativeArray<T> and Unity.Collections.NativeSlice<T>.
NativeList | A resizable list. |
UnsafeList | A resizable list. |
UnsafePtrList | A resizable list of pointers. |
NativeStream | A set of append-only, untyped buffers. |
UnsafeStream | A set of append-only, untyped buffers. |
UnsafeAppendBuffer | An append-only untyped buffer. |
NativeQueue | A resizable queue. |
UnsafeRingQueue | A fixed-size circular buffer. |
FixedList32Bytes | A 32-byte list, including 2 bytes of overhead, so 30 bytes are available for storage. Max capacity depends upon the type parameter. |
FixedList64Bytes | A 64-byte list, including 2 bytes of overhead, so 62 bytes are available for storage. Max capacity depends upon the type parameter. |
FixedList128Bytes | A 128-byte list, including 2 bytes of overhead, so 126 bytes are available for storage. Max capacity depends upon the type parameter. |
FixedList512Bytes | A 512-byte list, including 2 bytes of overhead, so 510 bytes are available for storage. Max capacity depends upon the type parameter. |
FixedList4096Bytes | A 4096-byte list, including 2 bytes of overhead, so 4094 bytes are available for storage. Max capacity depends upon the type parameter. |
没有多维数组类型,但是可以把多维转成一维,比如: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
NativeParallelHashMap | An unordered associative array of key-value pairs. |
UnsafeParallelHashMap | An unordered associative array of key-value pairs. |
NativeParallelHashSet | A set of unique values. |
UnsafeParallelHashSet | A set of unique values. |
NativeMultiHashMap | An unordered associative array of key-value pairs. The keys do not have to be unique, i.e. two pairs can have equal keys. |
UnsafeMultiHashMap | An 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
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
NativeText | 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. |
See also FixedString and FixedStringMethods.
Other types
NativeReference | 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.
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
.