Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


NativeArray<T>

Provides a buffer of native memory to managed code, making it possible to share data between managed and native code without marshalling costs.
Read time 3 minutesLast updated 6 days ago

Definition

public struct NativeArray<T> : IDisposable, IEnumerable<T>, IEnumerable, IEquatable<NativeArray<T>> where T : struct

Remarks

NativeArray is a fixed-size block of unmanaged memory which you can directly access from managed code. You can use NativeArray instances in jobs and Burst-compiled code, with optional safety checks for bounds, access, and dependencies. You explicitly control allocation and disposal via an allocator, and Unity tracks allocations to help detect memory leaks. You can use the Unity Collections package to further extend its functionality.

Constructors

Constructor

Description

NativeArray`1(`0[],Unity.Collections.Allocator)Creates a NativeArray from an array of elements.
NativeArray`1(`0[],Unity.Collections.MemoryLabel)Creates a NativeArray from an array of elements, using the specified memory label.
NativeArray`1(System.Int32,Unity.Collections.Allocator,Unity.Collections.NativeArrayOptions)Creates a new NativeArray and allocates enough memory to fit the provided amount of elements.
NativeArray`1(System.Int32,Unity.Collections.MemoryLabel,Unity.Collections.NativeArrayOptions)Creates a new NativeArray and allocates enough memory to fit the provided number of elements, using the specified memory label.
NativeArray`1(Unity.Collections.NativeArray{`0},Unity.Collections.Allocator)Creates a NativeArray from an existing NativeArray.
NativeArray`1(Unity.Collections.NativeArray{`0},Unity.Collections.MemoryLabel)Creates a NativeArray from an existing NativeArray, using the specified memory label.

Properties

Property

Description

IsCreatedIndicates that a NativeArray<T> has an allocated memory buffer.
this[]Access NativeArray<T> elements by index.
LengthNumber of elements in a NativeArray<T>.

Methods

Method

Description

AsReadOnlyCasts a NativeArray<T> to read-only array.
AsReadOnlySpanExposes NativeArray<T> data as a
System.ReadOnlySpan<T>
.
AsSpanExposes NativeArray<T> data as a
System.Span<T>
.
CopyFromCopies all the elements from a NativeArray<T> or a managed array of the same length.
CopyToCopies all the elements to another NativeArray<T> or a managed array of the same length.
DisposeDisposes a NativeArray<T>.
EqualsCompares a NativeArray<T> instance and an object.
GetEnumeratorGets an enumerator.
GetHashCodeGets a hash code for the current instance.
GetSubArrayGets a view into an array starting at the specified index.
ReinterpretReinterpret a NativeArray<T> with a different data type (type punning).
ReinterpretLoadReinterpret and load data starting at underlying index as a different type.
ReinterpretStoreReinterpret and store data starting at underlying index as a different type.
ToArrayConvert a NativeArray<T> to an array.

Static Methods

Method

Description

CopyCopies a range of elements from a source array to a destination array, starting from the source index and copying them to the destination index.

Structs

Struct

Description

NativeArray<T>.ReadOnlyRepresents a NativeArray<T> interface constrained to read-only operations.