Data can only be retrieved from jobs via native collections.
This means you need to:
See collection types for a complete overview of native collections.
Type | Description | Requires capacity | Supports parallel writes |
---|---|---|---|
NativeArray<T> |
A fixed-length data structure similar to a C# array (T[] ). |
No | Yes |
NativeList<T> |
A data structure similar to a List<T> . |
No | Partial (no resizing) |
NativeQueue<T> |
A data structure similar to a Queue<T> . |
No | Yes |
NativeHashSet<T> NativeParallelHashSet<T> |
A data structure similar to a HashSet<T> . |
Yes | Yes |
NativeHashMap<TKey, TValue> NativeParallelHashMap<TKey, TValue> |
A data structure similar to a Dictionary<TKey, TValue> . |
Yes | Yes |
NativeMultiHashMap<TKey, TValue> NativeParallelMultiHashMap<TKey, TValue> |
A data structure similar to a Dictionary<TKey, List<TValue>> . |
Yes | Yes |
For situations requiring parallel writing either the capacity must be specified beforehand, or the order will not be maintained. If order is required with complex structures the best approach can differ between choosing to allocate extra capacity, using double-buffering techniques to grow buffers over multiple iterations, or running multiple jobs to collect and post-process data.
Many custom structures exist that act as native bridges. You can search sources like GitHub for custom native collections such as native trees.
The NativeReference
is the same as a NativeArray
of length 1
, but it semantically implies a single value.
public void RunJob()
{
NativeArray<float> output = new(10, Allocator.TempJob);
var job = new MyJob
{
Output = output,
Multiplier = 100
};
JobHandle handle = jobData.Schedule();
handle.Complete();
Read output// --- Read output ---
foreach(float value in output)
{
Debug.Log(value);
}
output.Dispose();
}
public struct MyJob : IJob
{
public NativeArray<float> Output;
public float Multiplier;
public void Execute()
{
Example usage// --- Example usage ---
int length = Output.Length;
for (int i = 0; i < length; i#)
{
Output[i] = i * Multiplier;
}
}
}
Note that immediately completing a job is not ideal, see the create and run a job example in the Unity documentation.